Drazen
Drazen

Reputation: 2826

Rspec returns results only after second test run

I'm testing my REST API and the issue i have is that i create a video, using factory girl and i only get a result from the API the second time i run the test. Even if i comment out the video creation after the first run, so there is no second video created.

This only works when i configure

config.use_transactional_fixtures = false

so test data will stay in the database.

My Spec file

describe "API Version 1" do
  describe "Videos" do

let(:video) { FactoryGirl.create(:video) }
before { get api_v1_videos_path }

it "should work" do
  response.status.should be(200)
end

it "should return the video" do
    output = JSON.parse(response.body)
    output.should == video.title
end

end
end

Error after first run

1) API Version 1 Videos should return the video
 Failure/Error: output.should == video.title
   expected: "Darknight"
        got: [] (using ==)
 # ./spec/requests/api/v1_spec.rb:15:in `block (3 levels) in <top (required)>'

Error after 2nd run (still error but returns results)

1) API Version 1 Videos should return the video
 Failure/Error: output.should == video.title
   expected: "Darknight"
        got: [{"id"=>3, "title"=>"Darknight", "video"=>"thevideo.mp4", "stream"=>"playlist.m3u8", "poster"=>"/uploads/test/video/3_darknight/poster/testimage.jpg", "categories"=>[{"id"=>3, "title"=>"test category"}]}] (using ==)
   Diff:
   @@ -1,2 +1,7 @@
   -"Darknight"
   +[{"id"=>3,
   +  "title"=>"Darknight",
   +  "video"=>"thevideo.mp4",
   +  "stream"=>"playlist.m3u8",
   +  "poster"=>"/uploads/test/video/3_darknight/poster/testimage.jpg",
   +  "categories"=>[{"id"=>3, "title"=>"test category"}]}]
 # ./spec/requests/api/v1_spec.rb:15:in `block (3 levels) in <top (required)>'

Seems like rspec is not visiting the API after the video is created?

Upvotes: 2

Views: 144

Answers (1)

Norto23
Norto23

Reputation: 2269

Change the below line...

let(:video) { FactoryGirl.create(:video) }

to

let!(:video) { FactoryGirl.create(:video) }

Upvotes: 1

Related Questions