【问题标题】:Rspec returns results only after second test runRspec 仅在第二次测试运行后返回结果
【发布时间】:2012-06-12 21:51:07
【问题描述】:

我正在测试我的 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?

【问题讨论】:

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


    【解决方案1】:

    更改以下行...

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多