I'm just getting started with Rails and I'm writing some unit tests for a Todo list app with RSpec. I've got the REST API written and working, but now I can't seem to figure out an issue with a test. Here's the spec:
require 'rails_helper'
describe "Lists API" do
context "from start" do
it 'is empty' do
get "/lists"
expect(response).to be_success
expect(json.size).to eq(0)
end
it 'can create Lists' do
post "/lists", :list => {:title => "First List", :status => "Unstarted"}
expect(response).to be_success
post "/lists", :list => {:title => "Second List", :status => "Unstarted"}
expect(response).to be_success
#lines only here to show the problem only exists in a different test block
get "/lists"
expect(response).to be_success
expect(json.size).to eq(2)
end
end
context "once populated" do
it 'can view created lists' do
get "/lists"
expect(response).to be_success
expect(json.size).to eq(2)
end
end
end
Then when I run RSpec, I get this error:
Failures:
1) Lists API once populated can view created lists
Failure/Error: expect(json.size).to eq(2)
expected: 2
got: 0
(compared using ==)
It seems like the database is getting emptied for each it
block. Is that correct? Is there any way to have a fresh database for each describe
but not have it emptied for each it
?