水豚 + RSpec 仅在控制器规格中看到空白页。为什么?

发布于 2024-11-05 16:59:21 字数 343 浏览 1 评论 0原文

我正在尝试为一个简单的控制器编写控制器规范。但是,Capybara 看不到任何页面内容。但是,在我的浏览器中查看该网站的页面效果很好。我做错了什么?

谢谢!

我的控制器规范

我的spec_helper.rb

我的 Gemfile

I'm trying to write a controller spec for a simple controller. However, Capybara isn't seeing any page content. However, looking at the site's pages in my browser works just fine. What am I doing wrong?

T. Hanks!

My controller spec

My spec_helper.rb

My Gemfile

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

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

发布评论

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

评论(1

清风无影 2024-11-12 16:59:21

您需要明确告诉您的控制器规范您希望它渲染视图才能使其工作。将您的规范更新为如下所示:

require 'spec_helper'

describe PostsController do
  render_views # Render this controller's views during spec execution.

  before do
    @post = Fabricate :post
  end

  # ...
end

rspec 的 自述文件 中对此进行了描述。有关更详细的视图,请参阅 rspec-rails 的 cucumber “render_views”功能< /a>.

对此,请注意一点。这不是默认行为是有原因的:

  • 可以说,您通过与控制器同时测试视图来混合两个问题。 Ryan Bigg(参见评论)建议您的测试可能最好被视为集成测试,它通常位于spec/integration而不是spec/controller
  • 渲染视图可能会大大减慢测试的执行速度。

...并不是说你不应该这样做,只是说你应该清楚为什么

希望有帮助。

You need to explicitly tell your controller spec that you want it to render views in order for this to work. Update your spec to look like this:

require 'spec_helper'

describe PostsController do
  render_views # Render this controller's views during spec execution.

  before do
    @post = Fabricate :post
  end

  # ...
end

This is described in rspec's readme. For a more detailed view, see rspec-rails' cucumber feature for 'render_views'.

Just one word of caution with this. There are reasons why this isn't default behaviour:

  • Arguably, you're mixing two concerns by testing the views at the same time as the controllers. Ryan Bigg (see comments) suggests your tests might be better thought of as integration tests, which usually live in spec/integration rather than spec/controller.
  • Rendering the views may slow down the execution of your tests considerably.

... Not saying you shouldn't do this, just saying you should be clear why you are.

Hope that helps.

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