测试使用Devise与RSpec的视图 [英] Testing Views that use Devise with RSpec

查看:142
本文介绍了测试使用Devise与RSpec的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得一个以前通过的rspec查看规范,将Devise的 user_signed_in?方法添加到有问题的视图模板中。模板看起来像这样:

I am trying to get a previously passing rspec "view spec" to pass after adding Devise's user_signed_in? method to the view template in question. The template looks something like this:

<% if user_signed_in? %>
  Welcome back.
<% else %>
  Please sign in.
<% endif %>

传递的视图规范如下所示:

The view spec that was passing looks something like this:

require "spec_helper"

describe "home/index.html.erb" do

  it "asks you to sign in if you are not signed in" do
    render
    rendered.should have_content('Please sign in.')
  end

end

将调用添加到 user_signed_in? code>是:

The error it produces after adding the call to user_signed_in? is:

  1) home/index.html.erb asks you to sign in if you are not signed in
     Failure/Error: render
     ActionView::Template::Error:
       undefined method `authenticate' for nil:NilClass
     # ./app/views/home/index.html.erb:1:in `_app_views_home_index_html_erb__1932916999377371771_70268384974540'
     # ./spec/views/home/index.html.erb_spec.rb:6:in `block (2 levels) in <top (required)>'

有很多参考文献o网络上的这个错误,但是我还没有找到一个足够的答案,我可以再次通过测试。我认为这个问题与视图(这是正在独立测试的任何型号/控制器)没有一些关键的Devise基础架构有关。您的建议不胜感激。

There are plenty of references to this error around the web, but I have yet to find an answer descriptive enough that I can get my test passing again. I believe the problem has something to do with the view (which is being testing in isolation from any models/controllers) not having some key Devise infrastructure available. Your suggestions are appreciated.

此外,一旦测试通过,我将如何测试其他路径(用户已登录)?我认为这将非常相似。谢谢。

Also, once the test passes, how would I test the other path (user already signed in)? I presume it will be very similar. Thanks.

推荐答案

您收到的错误是因为您需要包含设计测试助手

The error you're receiving is because you need to include the devise test helpers

一般来说,您将添加(或许已经有)spec / support / devise.rb

Generally you'll add this (and you might already have) to spec/support/devise.rb

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

但是,由于您创建了一个视图规范,您将需要如下所示:

But since you're creating a view spec, you'll want something like this:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
  config.include Devise::TestHelpers, :type => :view
end

这篇关于测试使用Devise与RSpec的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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