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

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

问题描述

根据从devise wiki我应该能够在我的控制器测试中使用一个login_user帮助程序。因此,我在规格目录中有以下内容:

 #spec_helper.rb 
...
目录[Rails.root.join(spec / support / ** / *。rb)]。 require f}

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

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

但是调用助手不起作用:



$ $ $ $ $ $ $ $ $需要'spec_helper'
描述GET / guides / editdo
login_user
end

有人可以指向我错在哪里。测试套件从此开始工作。我得到一个未定义的局部变量或方法消息,所以我猜是没有包含该模块。



Rails 3.0.7
rspec 2.6.0
设计1.3.4



回溯

解决方案

我想象这种方法有几个问题。首先是使用请求规范而不是控制器规范,所以 login_user 方法不能通过 config.extend ControllerMacros,:type = > :控制器。第二,即使您能够包含该方法,也很有可能无法正常工作,因为Devise测试帮助者专门为控制器/视图测试而编写,而不是集成测试。



看看David Chelimsky的答案这个SO问题,这可能是有帮助的。


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
...

and

#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 rspec 2.6.0 devise 1.3.4

backtrace

解决方案

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.

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

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

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