gpt4 book ai didi

ruby-on-rails - 帮助追踪 Controller 规范失败的原因

转载 作者:行者123 更新时间:2023-12-04 06:33:00 25 4
gpt4 key购买 nike

我有一个失败的 user_controller_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 时,它说:
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

在浏览器中转到/home/页面工作正常。

有没有办法获得更详细的失败原因?

注:

这是 rails3,我使用的是 rspec。

我也有 capybara gem,搜索我的解决方案显示 capybara 的唯一引用是在我的 gem 和 gem.lock 文件中。

最佳答案

您可以尝试输出响应正文以查看消息是什么。可能是您登录的用户没有正确的权限(或匿名访问您必须登录才能查看的页面)到测试环境中的奇怪 View 错误。

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 be_redirect .

关于ruby-on-rails - 帮助追踪 Controller 规范失败的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6050256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com