rspec 与 devise 的集成测试抛出 NoMethodError 错误

发布于 2024-12-28 22:23:51 字数 4057 浏览 0 评论 0原文

我在这里发布了类似的错误,但所有这些都无法解决我的问题。

我的文件“spec/request/news_controller_spec.rb”如下所示:

require 'spec_helper'

describe "NewsController" do

  include Devise::TestHelpers

  describe "GET /news" do

    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @user = Factory.create(:user)
      @user.confirm!
      sign_in @user
    end

    it "after login should show the latest news an screen" do
      page.should have_content("News")
    end
  end
end

如果我运行测试,我会得到:

Failures:

  1) Campaigns GET /news works!
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `env' for nil:NilClass
     # /Users/.../gems/devise-1.5.3/lib/devise/test_helpers.rb:25:in `setup_controller_for_warden'
     # /Users/.../gems/rspec-rails-2.8.1/lib/rspec/rails/adapters.rb:15:in `block (2 levels) in setup'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:35:in `instance_eval'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:35:in `run_in'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `block in run_all'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `each'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `run_all'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:368:in `run_hook'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `block in run_before_each_hooks'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `each'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `run_before_each_hooks'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example.rb:217:in `run_before_each'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example.rb:79:in `block in run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example.rb:173:in `with_around_hooks'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example.rb:77:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:355:in `block in run_examples'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `map'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `run_examples'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:337:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `block in run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `map'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/reporter.rb:34:in `report'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'

我按照 https://github.com/plataformatec/devise/wiki/How-To%3a-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29和其他类似的问题在这里,但我找不到解决方案。

我正在使用以下宝石集:

  • capybara(1.1.2)
  • devise(1.5.3)
  • factory_girl_rails(1.6.0)
  • rails(3.2.0,3.1.1)
  • rspec(2.8.0)
  • rspec-core(2.8.0)
  • rspec -期望(2.8.0)
  • rspec-mocks(2.8.0)
  • rspec-rails(2.8.1)

和一些其他的宝石。

任何人都可以帮助我,让登录用户运行我的集成测试。在这种情况下,仅当用户登录时才会显示新闻。

谢谢。

I have a similar error posted here, but all will not fix my problem.

My file 'spec/request/news_controller_spec.rb' looks like:

require 'spec_helper'

describe "NewsController" do

  include Devise::TestHelpers

  describe "GET /news" do

    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @user = Factory.create(:user)
      @user.confirm!
      sign_in @user
    end

    it "after login should show the latest news an screen" do
      page.should have_content("News")
    end
  end
end

If I run the test I'll get:

Failures:

  1) Campaigns GET /news works!
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `env' for nil:NilClass
     # /Users/.../gems/devise-1.5.3/lib/devise/test_helpers.rb:25:in `setup_controller_for_warden'
     # /Users/.../gems/rspec-rails-2.8.1/lib/rspec/rails/adapters.rb:15:in `block (2 levels) in setup'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:35:in `instance_eval'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:35:in `run_in'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `block in run_all'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `each'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `run_all'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:368:in `run_hook'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `block in run_before_each_hooks'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `each'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `run_before_each_hooks'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example.rb:217:in `run_before_each'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example.rb:79:in `block in run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example.rb:173:in `with_around_hooks'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example.rb:77:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:355:in `block in run_examples'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `map'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `run_examples'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:337:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `block in run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `map'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/reporter.rb:34:in `report'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
     # /Users/.../gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'

I followed instructions on https://github.com/plataformatec/devise/wiki/How-To%3a-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29 and other similar Questions here, but I can't find a solution.

I'm using following gemset:

  • capybara (1.1.2)
  • devise (1.5.3)
  • factory_girl_rails (1.6.0)
  • rails (3.2.0, 3.1.1)
  • rspec (2.8.0)
  • rspec-core (2.8.0)
  • rspec-expectations (2.8.0)
  • rspec-mocks (2.8.0)
  • rspec-rails (2.8.1)

and some other gems.

Can anybody help me, getting my integration tests with a logged in user get running. In this case the news are only displayed, if a user is logged in.

Thanks.

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

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

发布评论

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

评论(1

各自安好 2025-01-04 22:23:51

我实际上也遇到了同样的问题,经过一番谷歌搜索后,我发现直接包含 Warden::Test::Helpers 是请求规范的更简洁的解决方案,因为它以编程方式登录/注销用户就像这样:

# spec/support/request_helpers.rb
require 'spec_helper'
include Warden::Test::Helpers

module RequestHelpers
  def create_logged_in_user
    user = Factory(:user)
    login(user)
    user
  end

  def login(user)
    login_as user, scope: :user
  end
end

简而言之,只需包含 Warden::Test::Helpers ,然后使用 login_as 方法即可!

从这里获取的解决方案: http://simple10.com/rspec-capybara-devise-login -测试/

I actually had the same exact problem, and after googling a little bit I found that including Warden::Test::Helpers directly was a more concise solution for request specs, since it programmatically signs in/out users just like this:

# spec/support/request_helpers.rb
require 'spec_helper'
include Warden::Test::Helpers

module RequestHelpers
  def create_logged_in_user
    user = Factory(:user)
    login(user)
    user
  end

  def login(user)
    login_as user, scope: :user
  end
end

In short, just include Warden::Test::Helpers and then use the login_as <your_user> method and that's it!

Solution taken from here: http://simple10.com/rspec-capybara-devise-login-tests/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文