帮助追踪控制器规范失败的原因

发布于 2024-11-08 14:13:20 字数 752 浏览 0 评论 0原文

我有一个 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。

我也有水豚 gem,搜索我的解决方案显示对水豚的唯一引用位于我的 gem 和 gem.lock 文件中。

I have a user_controller_spec.rb that is failing, and I'm not sure why.

require 'spec_helper'

describe UsersController do

  describe "GET 'index'" do
    it "should be successful" do
      get 'index'
      response.should be_success
    end
  end


end

When I run rspec it says:

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

Going to the /home/ page in the browser works fine.

Is there a way to get a more detailed reason why it is failing?

Note:

This is rails3, and I am using rspec.

I also have the capybara gem, and searching my solution shows the only reference to capybara is in my gem and gem.lock file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍白女子 2024-11-15 14:13:20

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

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

You can try outputting the response body to see what the message is. Could be anything from the user you're logged in as not having the correct permissions (or visiting a page anonymously that you must be logged in to see) to a strange view error in test environment.

get 'index'
puts response.body.inspect
puts response.status.inspect
...
response.should be_success

response.body will contain the HTML output of the response, so you should be able to tell why it's not a success (hopefully it will have a stack trace or be a redirect or something). Also keep in mind redirecting is not "success". If I remember correctly be_success makes sure the HTTP status code is one of the 200s, redirects are usually 302 or 304 so do not count. If a redirect is intended, try response.should be_redirect.

自由如风 2024-11-15 14:13:20

您可能不只是渲染页面,而是重定向。为了检查可能出现的问题,我会在规范中执行类似以下操作:

response.should == 1

以便查看实际响应是什么。这将为您提供有关正在发生的事情的良好线索。

It could be that you do not just render the page, but redirect. To check on what may be wrong, i would do in my spec something like :

response.should == 1

in order to see what the actual response is. This would give you a good clue on what is happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文