使用 Devise 进行控制器测试的问题

发布于 2024-11-26 21:44:50 字数 1377 浏览 0 评论 0原文

我在 stackoverflow 上的第一篇文章...耶!无论如何,我正在尝试对 Ruby 192 和 Rails 3.0 上的设备进行简单的控制器测试。我在网上查了很多东西,但我被困住了。请帮帮我。 这是代码片段

require 'spec_helper'
require 'devise/test_helpers'

describe AttributeFunnel do
   include Devise::TestHelpers

  before(:each) do
    @request.env["devise.mapping"] = Devise.mappings[:admin]
  end

  it "should restrict access" do
    @user = Factory.build(:user, :email => "[email protected]",:password =>"abc123",:password_confirmation => "abc123" ,:agree_terms => 1)
    @user.save(:validate => false)
    @user.confirm!
    sign_in(@user)
  end
end

这是错误>>

NoMethodError: undefined method `request' for nil:NilClass
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.4/lib/active_support/whiny_nil.rb:48:in `method_missing'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/devise-1.2.rc/lib/devise/test_helpers.rb:27:in `initialize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/devise-1.2.rc/lib/devise/test_helpers.rb:67:in `new'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/devise-1.2.rc/lib/devise/test_helpers.rb:67:in `warden'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-rails-2.5.0/lib/rspec/rails/adapters.rb:11:in `block (2 levels) in setup'

我把 @request.env 作为最后的手段。仍然无法工作:(

My first post on stackoverflow...Yeeh !. Anyways I'm trying to do a simple controller test for devise on Ruby 192 and Rails 3.0. I went through a lot of stuff on the web but I'm stuck. Please help me out.
Here is the code snippet

require 'spec_helper'
require 'devise/test_helpers'

describe AttributeFunnel do
   include Devise::TestHelpers

  before(:each) do
    @request.env["devise.mapping"] = Devise.mappings[:admin]
  end

  it "should restrict access" do
    @user = Factory.build(:user, :email => "[email protected]",:password =>"abc123",:password_confirmation => "abc123" ,:agree_terms => 1)
    @user.save(:validate => false)
    @user.confirm!
    sign_in(@user)
  end
end

This is the error >>

NoMethodError: undefined method `request' for nil:NilClass
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.4/lib/active_support/whiny_nil.rb:48:in `method_missing'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/devise-1.2.rc/lib/devise/test_helpers.rb:27:in `initialize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/devise-1.2.rc/lib/devise/test_helpers.rb:67:in `new'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/devise-1.2.rc/lib/devise/test_helpers.rb:67:in `warden'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-rails-2.5.0/lib/rspec/rails/adapters.rb:11:in `block (2 levels) in setup'

I put the @request.env as a last resort. Still not working :(

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文