Rspec仅在第二次测试运行后返回结果

时间:2012-06-12 21:51:07

标签: ruby-on-rails rspec integration-testing

我正在测试我的REST API,我遇到的问题是我创建了一个视频,使用工厂女孩,我只在第二次运行测试时从API获得结果。即使我在第一次运行后注释掉视频创建,也没有创建第二个视频。

这仅在配置

时有效
config.use_transactional_fixtures = false

因此测试数据将保留在数据库中。

我的规范文件

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

首次运行后出错

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)>'

第二次运行后出错(仍然是错误但返回结果)

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)>'

看起来像rspec在创建视频后没有访问API?

1 个答案:

答案 0 :(得分:1)

更改以下行...

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

let!(:video) { FactoryGirl.create(:video) }
相关问题