ruby-on-rails - Capybara + RSpec 只能在 Controller 规范中看到空白页。为什么?

标签 ruby-on-rails ruby tdd rspec capybara

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

T。汉克斯!

My controller spec

My spec_helper.rb

My Gemfile

最佳答案

您需要明确地告诉您的 Controller 规范您希望它渲染 View 才能使其正常工作。将您的规范更新为如下所示:

require 'spec_helper'

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

  before do
    @post = Fabricate :post
  end

  # ...
end

这在 rspec 的 readme 中有描述.有关更详细的 View ,请参阅 rspec-rails 的 cucumber feature for 'render_views' .

对此只有一个警告。这不是默认行为是有原因的:

  • 可以说,您在测试 View 和 Controller 的同时测试了两个问题。 Ryan Bigg(见评论)建议您的测试可能更好地被认为是集成测试,它通常存在于 spec/integration 而不是 spec/controller.
  • 呈现 View 可能会大大减慢测试的执行速度。

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

希望对您有所帮助。

关于ruby-on-rails - Capybara + RSpec 只能在 Controller 规范中看到空白页。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5932605/

相关文章:

c++ - c++ using 声明的 ruby​​ 等价物是什么?

ruby-on-rails - 我无法理解此错误消息

ruby-on-rails - 优雅的 Rails : multiple routes, 相同的 Controller Action

ruby-on-rails - Rspec 使用 has_many throw 方法删除对象失败,没有路由匹配 { :action= >"destroy"

jquery - rails 和 html 数据属性 : use dash(-) or underscore(_)?

jquery - sprockets::StaticPages#home 中未找到文件

c# - 从屏幕继承的单元测试 View 模型 (Caliburn.Micro)

ruby - 对记录的 Rspec stub 方法调用

c# - AutoMockContainer 支持具有非接口(interface)依赖性的自动模拟类

ruby-on-rails - 州、市、区数据库设计