规范与规范设计测试助手
根据这个 来自 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这种方法存在一些问题。首先,您使用的是请求规范,而不是控制器规范,因此 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 byconfig.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.
我无法肯定地回答......但对我来说,代码的味道是助手内部定义的“before(:each)”。你为什么不尝试:
如果
失败了 - 也许它只是找不到@request - 在这种情况下,将它作为变量传递给login_user
编辑:
看起来你可能需要包括设计测试助手。
rdoc 表示您应该拥有此文件:
不确定这是否与您已经获得的方式不同规范助手.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:
and
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:
Not sure if that differs from how you've already got it in spec_helper.rb
... looks pretty similar to me.
我的控制器规格上的 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
toconfig.include
and its work !如果您的应用程序不可确认,请忘记确认。你的代码应该看起来像
Forget to confirm if your app is not confirmable. Your code should look like