Ruby on rails 帮助跟踪控制器规范失败的原因
我有一个user\u controller\u spec.rb失败了,我不知道为什么Ruby on rails 帮助跟踪控制器规范失败的原因,ruby-on-rails,controller,rspec,Ruby On Rails,Controller,Rspec,我有一个user\u controller\u spec.rb失败了,我不知道为什么 require 'spec_helper' describe UsersController do describe "GET 'index'" do it "should be successful" do get 'index' response.should be_success end end end 当我运行rspec时,它会说: Failur
require 'spec_helper'
describe UsersController do
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
end
end
当我运行rspec时,它会说:
Failures:
1) UsersController GET 'index' should be successful
Failure/Error: response.should be_success
expected success? to return true, got false
# ./spec/controllers/users_controller_spec.rb:8
Finished in 0.17047 seconds
1 example, 1 failure
在浏览器中转到/主页可以正常工作
有没有办法得到更详细的失败原因
注意:
这是rails3,我使用的是rspec
我也有capybara gem,搜索我的解决方案时会发现对capybara的唯一引用在我的gem和gem.lock文件中。您可以尝试输出响应正文以查看消息内容。可能是从您登录的用户没有正确的权限(或匿名访问您必须登录才能看到的页面)到测试环境中的奇怪视图错误
get 'index'
puts response.body.inspect
puts response.status.inspect
...
response.should be_success
response.body
将包含响应的HTML输出,因此您应该能够告诉它为什么不成功(希望它有堆栈跟踪或重定向之类的)。还要记住,重定向不是“成功”。如果我没记错的话,be_success
确保HTTP状态代码是200之一,重定向通常是302或304,因此不算在内。如果要重定向,请尝试response.should\u redirect
这可能是因为您不只是呈现页面,而是重定向。为了检查可能的错误,我会在我的规范中执行以下操作:
response.should == 1
以查看实际响应是什么。这会给你一个很好的线索,告诉你发生了什么。好了,现在可以了,谢谢(修复了一些问题,它是重定向的),但是如果我做了响应,它总是失败。应该==1它做什么?是的,这一个是故意失败的:)你只是用它来检查你得到的实际值是多少(它会说:expected:success,get:redirect。非常好的提示,谢谢!顺便说一句,可以访问请求对象以查看原始url请求等?