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