2012-06-12 75 views
2

我正在测试我的REST API,我遇到的问题是我使用工厂女孩创建了一个视频,而且第二次运行测试时我只能从API获取结果。即使我在第一次运行后注释了视频创建,也不会创建第二个视频。Rspec仅在第二次测试运行后才返回结果

这只有当我配置

config.use_transactional_fixtures = false 

这样的测试数据将保留在数据库中运行。

我的SPEC文件

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

更改以下行...

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

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

非常感谢你,这个 “!”爆炸花了我近4个小时 - .- 你能否迅速解释它为什么现在工作? –

+0

不用担心!啊,编码的乐趣。 – Norto23

+2

区别在这里解释:https://www.relishapp.com/rspec/rspec-core/v/2-4/docs/helper-methods/let-and-let – MrDanA

相关问题