2012-06-12 78 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

相關問題