rspec &设计测试助手 [英] rspec & devise test helpers

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

问题描述

根据 this 来自设计 wiki 我应该能够在我的控制器测试中使用 login_user 帮助方法.因此,我在规范目录中有以下内容:

According to this from the devise wiki I should be able to use a login_user helper method in my controller tests. Accordingly I have the following within the spec directory:

#spec_helper.rb
...
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

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

#support/controller_macros.rb
module ControllerMacros    
  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @user = Factory.create(:user)
      sign_in @user
    end
  end
end

但是调用助手不起作用:

however calling the helper doesn't work:

#requests/some_spec.rb
require 'spec_helper'
describe "GET /guides/edit" do
  login_user     
end

谁能指出我哪里出错了.测试套件从此开始工作.我收到一个未定义的局部变量或方法消息,所以我猜该模块没有被包含.

Can someone point toward where I'm going wrong. The test suite works about from this. I get a undefined local variable or method message so I guess the module isn't being included.

Rails 3.0.7规范 2.6.0设计1.3.4

Rails 3.0.7 rspec 2.6.0 devise 1.3.4

回溯

推荐答案

我认为这种方法存在一些问题.首先是您使用的是请求规范,而不是控制器规范,因此 config.extend ControllerMacros, :type => 无法使 login_user 方法可用.:控制器.其次,即使您能够包含该方法,它也很可能无法正常工作,因为 Devise 测试助手是专门为控制器/视图测试而不是集成测试编写的.

I imagine there are a couple of problems with this approach. First is that you're using request specs, not controller specs, so the login_user method is not made available by config.extend ControllerMacros, :type => :controller. Second, even if you are able to include the method it most likely won't work anyway, since the Devise test helpers are specifically written for controller/view tests, not integration tests.

看看 David Chelimsky 的回答this SO question,可能有帮助.

Take a look at David Chelimsky's answer to this SO question, which may be of help.

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

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