NameError:未定义的局部变量或方法`logger'

时间:2022-02-22 11:56:07

When I run 'script/server' everything works fine, but when I run my unit tests (rake test:units), I get the error below, and am not sure how to solve this.

当我运行'script / server'时一切正常,但是当我运行我的单元测试(rake test:units)时,我得到下面的错误,我不知道如何解决这个问题。

Error

NameError: undefined local variable or method `logger' for #<GiveawayEligibleMemberTest:0x10477dff8>
    /Users/kamilski81/Sites/pe/vitality_mall/vendor/rails/actionpack/lib/action_controller/test_process.rb:471:in `method_missing'
    /Users/kamilski81/Sites/pe/vitality_mall/lib/update_giveaway_eligible_members.rb:17:in `is_valid_checksum?'
    /Users/kamilski81/Sites/pe/vitality_mall/test/unit/giveaway_eligible_member_test.rb:26:in `test_that_checksum_is_valid'
    /Users/kamilski81/Sites/pe/vitality_mall/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:60:in `__send__'
    /Users/kamilski81/Sites/pe/vitality_mall/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:60:in `run'

I tried putting:

我尝试过:

class Test::Unit::TestCase
  RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
  RAILS_DEFAULT_LOGGER.level = Logger::WARN

  logger = Logger.new(STDOUT)
  logger.level = Logger::WARN
end

Here is the code that is using my logger:

以下是使用我的记录器的代码:

def is_valid_checksum?(csv_arr)
  expected_row_count = csv_arr[0][3].to_i
  logger.debug "Expected record count: #{expected_row_count}"
  actual_row_count = csv_arr.nitems - 1
  logger.debug "Actual record count: #{actual_row_count}"
  checksum_valid = false
  if expected_row_count == actual_row_count
    logger.debug "Checksum is valid"
    checksum_valid = true
  end

  return checksum_valid
end

But this still does not solve the error

但这仍然无法解决错误

3 个解决方案

#1


43  

You can use the Rails logger outside of models and controllers:

您可以在模型和控制器之外使用Rails记录器:

Rails.logger.info "..."

Source

资源

#2


4  

The logger method isn't available to test cases instance methods.

记录器方法不适用于测试用例实例方法。

You have defined a local variable in your class definition but this isn't enough. The logger variable falls out of scope once the class is initialized. You may be looking for a class variable (@@logger). But a cleaner solution would be wrapping it in a method like this.

您已在类定义中定义了局部变量,但这还不够。初始化类后,记录器变量超出范围。您可能正在寻找一个类变量(@@ logger)。但更清洁的解决方案是将其包装在这样的方法中。

class Test::Unit::TestCase
  def logger
    RAILS_DEFAULT_LOGGER ||= Logger.new(STDOUT)
  end
end

Notice this code will use the default logger if it is available (which it should be). If this isn't desired, you can make your own just as easily.

请注意,此代码将使用默认记录器(如果可用)(应该是)。如果不需要,您可以轻松制作自己的产品。

def logger
  @logger ||= Logger.new(STDOUT)
end

#3


0  

you should use RAILS_DEFAULT_LOGGER.debug the constant in your test case is_valid_checksum? not the variable logger(class level variable ) which cannot be used in a instance method.

您应该在测试用例中使用RAILS_DEFAULT_LOGGER.debug常量is_valid_checksum?而不是在实例方法中不能使用的变量记录器(类级变量)。

I would suggest to wrap the code in a instance method something like

我建议将代码包装在类似的实例方法中

def logger
  logger = Logger.new(STDOUT)
  logger.level = Logger::WARN
  logger
end

and then use logger

然后使用记录器

#1


43  

You can use the Rails logger outside of models and controllers:

您可以在模型和控制器之外使用Rails记录器:

Rails.logger.info "..."

Source

资源

#2


4  

The logger method isn't available to test cases instance methods.

记录器方法不适用于测试用例实例方法。

You have defined a local variable in your class definition but this isn't enough. The logger variable falls out of scope once the class is initialized. You may be looking for a class variable (@@logger). But a cleaner solution would be wrapping it in a method like this.

您已在类定义中定义了局部变量,但这还不够。初始化类后,记录器变量超出范围。您可能正在寻找一个类变量(@@ logger)。但更清洁的解决方案是将其包装在这样的方法中。

class Test::Unit::TestCase
  def logger
    RAILS_DEFAULT_LOGGER ||= Logger.new(STDOUT)
  end
end

Notice this code will use the default logger if it is available (which it should be). If this isn't desired, you can make your own just as easily.

请注意,此代码将使用默认记录器(如果可用)(应该是)。如果不需要,您可以轻松制作自己的产品。

def logger
  @logger ||= Logger.new(STDOUT)
end

#3


0  

you should use RAILS_DEFAULT_LOGGER.debug the constant in your test case is_valid_checksum? not the variable logger(class level variable ) which cannot be used in a instance method.

您应该在测试用例中使用RAILS_DEFAULT_LOGGER.debug常量is_valid_checksum?而不是在实例方法中不能使用的变量记录器(类级变量)。

I would suggest to wrap the code in a instance method something like

我建议将代码包装在类似的实例方法中

def logger
  logger = Logger.new(STDOUT)
  logger.level = Logger::WARN
  logger
end

and then use logger

然后使用记录器