Capybara + RSpec 只能看到控制器规格中的空白页.为什么? [英] Capybara + RSpec only sees blank pages in controller specs. Why?

查看:20
本文介绍了Capybara + RSpec 只能看到控制器规格中的空白页.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.汉克斯!

我的控制器规范

我的 spec_helper.rb

我的 Gemfile

推荐答案

您需要明确告诉您的控制器规范您希望它呈现视图以使其工作.将您的规范更新为如下所示:

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

在 rspec 的自述文件中对此进行了描述.有关更详细的视图,请参阅 rspec-rails 的黄瓜 'render_views' 的功能.

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:

  • 可以说,您通过与控制器同时测试视图来混合两个关注点.Ryan Bigg(见评论)建议你的测试可能更好地被认为是集成测试,它通常存在于 spec/integration 而不是 spec/controller>.
  • 渲染视图可能会大大减慢测试的执行速度.
  • 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.

希望有所帮助.

这篇关于Capybara + RSpec 只能看到控制器规格中的空白页.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆