gpt4 book ai didi

带有设计的 rspec 集成测试引发 NoMethodError 错误

转载 作者:行者123 更新时间:2023-12-04 11:28:46 24 4
gpt4 key购买 nike

我在这里发布了一个类似的错误,但都不能解决我的问题。

我的文件“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 上的说明和其他类似的问题在这里,但我找不到解决方案。

我正在使用以下 gemset:
  • capybara (1.1.2)
  • 设计 (1.5.3)
  • factory_girl_rails (1.6.0)
  • 导轨 (3.2.0, 3.1.1)
  • rspec (2.8.0)
  • rspec 核心 (2.8.0)
  • rspec 期望 (2.8.0)
  • rspec-mocks (2.8.0)
  • rspec-rails (2.8.1)

  • 和其他一些 gem 。

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

    谢谢。

    最佳答案

    我实际上遇到了完全相同的问题,在谷歌搜索后我发现包括 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 <your_user>方法,就是这样!

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

    关于带有设计的 rspec 集成测试引发 NoMethodError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8996246/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com