测试设计与rspec的意见 [英] Testing devise views with rspec

查看:272
本文介绍了测试设计与rspec的意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成运行设计的意见导轨摹色器件:观点现在想对其进行测试。

I have generated Devise's views running rails g devise:views and would now like to test them.

这是我想出了:

require 'spec_helper'

describe "devise/sessions/new" do
    before do
        render
    end

    it "renders the form to log in" do
        rendered.should have_selector("form", action: user_session_path, method: :post) do |form|

        end
    end
end

有关渲染声明它给了我未定义的局部变量或方法'资源'周围的Googling 我发现我要补充后

For the render statement it gives me undefined local variable or method 'resource'. After googling around I found that I should add

@user.should_receive(:resource).and_return(User.new)

在渲染的语句 - 但它仍然给了我同样的错误,我真的不知道如何使用它

before the render statement - but it still gives me the same error, and I am not really sure how to use it.

我是什么做错了吗?感谢您的帮助。

What am I doing wrong? Thanks for your help.

推荐答案

和那么,有什么知道吗?我发现这个答案如果有人有类似的问题。解决的办法是包括在您的应用助手以下code:

And whaddya know? I found this answer where someone had a similar problem. The solution is to include the following code in your application helper:

def resource_name
  :user
end

def resource
  @resource ||= User.new
end

def devise_mapping
  @devise_mapping ||= Devise.mappings[:user]
end

这是因为遗赠是使用其控制器一定的辅助方法是必要的。如果我从我的规格访问的看法,但是,这些辅助方法不可用,因此我的测试失败。把应用助手里面的这些方法使它们在整个应用程序访问,包括我的规格,况且在测试通过!

This is necessary because devise is using certain helper methods in its controllers. If I access the views from my specs, however, these helper methods are not available, hence my tests fail. Putting these methods inside the application helper makes them accessible throughout the application, including my specs, and indeed the tests pass!

这篇关于测试设计与rspec的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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