我正在开发一个带有测试覆盖率的Rails 3.2应用程序,但由于某些原因,我的一些测试失败了。相同的规范适用于另一个模型。
使用Rspec,FactoryGirl,Shoulda-Matchers运行规范。
这是失败的规范:http://pastebin.com/8VWCAv79
错误是:
Failures:
1) PlacesController POST create when user is logged in with valid params redirects to the created place
Failure/Error: response.should redirect_to(Place.last)
Expected response to be a <:redirect>, but was <200>
# ./spec/controllers/places_controller_spec.rb:109:in `block (5 levels) in <top (required)>'
2) PlacesController POST create when user is logged in with valid params assigns a newly created place as @place
Failure/Error: assigns(:place).should be_persisted
expected persisted? to return true, got false
# ./spec/controllers/places_controller_spec.rb:104:in `block (5 levels) in <top (required)>'
3) PlacesController POST create when user is logged in with valid params creates a new Place
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
# ./spec/controllers/places_controller_spec.rb:96:in `block (5 levels) in <top (required)>'
Finished in 4.63 seconds
21 examples, 3 failures
Failed examples:
rspec ./spec/controllers/places_controller_spec.rb:107 # PlacesController POST create when user is logged in with valid params redirects to the created place
rspec ./spec/controllers/places_controller_spec.rb:101 # PlacesController POST create when user is logged in with valid params assigns a newly created place as @place
rspec ./spec/controllers/places_controller_spec.rb:95 # PlacesController POST create when user is logged in with valid params creates a new Place
完全相同的规范出现在另一个文件http://pastebin.com/r8HtAwSR中,正确传递,没有问题。
这些是控制器文件:
有谁能建议我如何解决这个问题?
答案 0 :(得分:0)
看起来好像是因为某种原因没有创建新地方(我的猜测是,它没有通过验证)。
尝试调试您的测试。我喜欢使用pry。只需添加到gemfile gem 'pry'
,运行bundle,然后就可以在执行中设置断点。
例如试试这个:
it "redirects to the created place" do
post :create, {:place => valid_attributes}
binding.pry
response.should redirect_to(Place.last)
end
执行将在您插入'binding.pry'的位置停止,您可以检查终端中的最后位置。