我正在尝试为一个简单的 Controller 编写 Controller 规范。但是,Capybara 没有看到任何页面内容。但是,在我的浏览器中查看该网站的页面效果很好。我做错了什么?
T。汉克斯!
最佳答案
您需要明确地告诉您的 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/