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

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

问题描述

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

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 而非规格/控制器

  • 渲染视图可能会大大降低测试的执行速度。

  • 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天全站免登陆