gpt4 book ai didi

ruby-on-rails - Rails 4.0 运行时错误 : @controller is nil: make sure you set it in your test's setup method

转载 作者:行者123 更新时间:2023-12-03 15:16:37 31 4
gpt4 key购买 nike

当我运行 Controller (或功能)测试时,即使相同的测试在 Rails 3.2 中运行良好,Rails 4.0 也无法自动实例化 Controller 实例 (@controller)。
有关如何开始解决此问题的任何建议?

示例输出:

 $ ruby -Ilib:test test/controllers/account_controller_test.rb
[Coveralls] Set up the SimpleCov formatter.
[Coveralls] Using SimpleCov's 'rails' settings.
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.5-compliant syntax, but you are running 2.1.3.

...

1) Error:
AccountControllerTest#test_activate_api_key:
NoMethodError: undefined method `before_filter' for AccountController:Class
app/controllers/account_controller.rb:47:in `<class:AccountController>'
app/controllers/account_controller.rb:46:in `<top (required)>'

2) Error:
AccountControllerTest#test_add_mugshot:
RuntimeError: @controller is nil: make sure you set it in your test's setup method.
test/functional_test_case.rb:26:in `post'
test/controller_extensions.rb:533:in `assert_request'
test/controller_extensions.rb:202:in `either_requires_either'
test/controller_extensions.rb:137:in `post_requires_login'
test/controllers/account_controller_test.rb:418:in `test_add_mugshot'

账户 Controller 定义:
class AccountController < ApplicationController
before_filter :login_required, :except => [

应用 Controller :
class ApplicationController < ActionController::Base

最佳答案

看来ApplicationController不是源自 ActionController::Base当它在 AccountController 的定义中使用时,所以添加 require之前 AccountController宣言:

# file app/controllers/account_controller.rb
require 'app/controllers/application_controller'

class AccountController < ApplicationController
before_filter :login_required, :except => [

关于ruby-on-rails - Rails 4.0 运行时错误 : @controller is nil: make sure you set it in your test's setup method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235932/

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