当我运行’脚本/服务器’时,一切都可以正常运行,但是当我运行单元测试(rake test:units)时,我会收到以下错误,不知道如何解决这个问题.
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'
我试着把
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
这是使用我的记录器的代码:
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
解决方法
您可以在模型和控制器之外使用Rails记录器:
Rails.logger.info "..."