Hello fellow Rubyists,
today I was writing some api tests using rack-test gem and everything was well, I had my context
blocks with before
blocks creating some mockup data using FactoryGirl
. World was bright and green
But all of a sudden I stopped getting the results from one of my api controllers. It turned out that when I create one specific model object with FactoryGirl
in the before block it doesnât get stored/persisted to the test db.
I checked it with persisted?
call and it returns true but when I call Model.all
(right after creating it, even in before
block) it gives me back an empty array(and this is why my api controller doesnât return anything since it to gets an empty array).
Checking the dev environment instead of test gives the correct results plus my other models created with FactoryGirl
donât have such a problem.
Could someone give me a piece of advice on what it might be?
Here is the code I have in my spec(if it helps at all):
require âspec_helperâ
`
describe â/api/v2/festivals/:id/faqâ, :type => :api do
describe âindexâ do
before(:each) do
festival1 = create :festival
festival2 = create :festival
faq1 = create :faq
faq2 = create :faq
festival1.faqs << faq1
end
context 'faqs index per festival viewable by the api users' do
let(:url) { "/api/v2/festivals/#{festival1.id}/faq.json" }
it 'json' do
get url
last_response.status.should eql 200
faqs = JSON.parse last_response.body
faqs.any? do |faq|
faq['id'] == faq1.id
end.should be_true
faqs.any? do |faq|
faq['id'] == faq2.id
end.should_not be_true
end
end
end
end`
faq1
and faq2
in this case donât get persistent in test environment but return true on .persisted?
call.