规范与规范设计测试助手

发布于 2024-11-16 04:45:09 字数 1037 浏览 1 评论 0原文

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

#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

调用帮助程序不起作用:

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

有人能指出我出错的地方吗?测试套件由此开始工作。我收到未定义的局部变量或方法消息,因此我猜该模块未包含在内。

铁轨3.0.7 rspec 2.6.0 设计 1.3.4

回溯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伤感在游骋 2024-11-23 04:45:09

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

查看 David Chelimsky 对 的回答 ://stackoverflow.com/questions/5787409/stubbing-authentication-in-request-spec">这个问题,可能有帮助。

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.

错爱 2024-11-23 04:45:09

我无法肯定地回答......但对我来说,代码的味道是助手内部定义的“before(:each)”。你为什么不尝试:

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

如果

#requests/some_spec.rb
require 'spec_helper'
describe "GET /guides/edit" do
  before(:each) do
    login_user     
  end
end

失败了 - 也许它只是找不到@request - 在这种情况下,将它作为变量传递给login_user

编辑:

看起来你可能需要包括设计测试助手。
rdoc 表示您应该拥有此文件:

# spec/support/devise.rb
RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

不确定这是否与您已经获得的方式不同规范助手.rb
……和我长得很像。

I can't answer for sure... but the code smell for me is the "before(:each)" defined inside the helper. why don't you try:

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

and

#requests/some_spec.rb
require 'spec_helper'
describe "GET /guides/edit" do
  before(:each) do
    login_user     
  end
end

and if that fails - maybe it just can't find @request - in which case, pass it as a variable to login_user

Edit:

Looks like you might need to include the devise test helpers.
The rdoc says you should have this file:

# spec/support/devise.rb
RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

Not sure if that differs from how you've already got it in spec_helper.rb
... looks pretty similar to me.

大姐,你呐 2024-11-23 04:45:09

我的控制器规格上的 Rails 3.0.10 rspec 2.6.0 devise 1.3.4 spork-0.9.0.rc9 也有同样的问题,我已经更改了配置。扩展到config.include及其工作!

I have same issue with Rails 3.0.10 rspec 2.6.0 devise 1.3.4 spork-0.9.0.rc9 on my controller specs, i have changed config. extend to config.include and its work !

动听の歌 2024-11-23 04:45:09

如果您的应用程序不可确认,请忘记确认。你的代码应该看起来像

module ControllerMacros
  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = FactoryGirl.create(:user)
      #user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
      sign_in user
    end
  end
end

Forget to confirm if your app is not confirmable. Your code should look like

module ControllerMacros
  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = FactoryGirl.create(:user)
      #user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
      sign_in user
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文