Back to Repositories

Testing Logger Configuration and Severity Levels in Resque

This test suite validates the logging functionality in Resque, focusing on logger configuration and message severity levels. It ensures proper logger initialization and verifies that different log severity levels (debug, info, error, fatal) function correctly with message passing.

Test Coverage Overview

The test suite provides comprehensive coverage of Resque’s logging capabilities.

Key areas tested include:
  • Logger instance assignment and retrieval
  • Message logging across multiple severity levels (debug, info, error, fatal)
  • Logger interface compatibility
The tests verify both the configuration aspects and runtime behavior of the logging system.

Implementation Analysis

The testing approach utilizes Minitest’s mocking capabilities to isolate logging behavior.

Technical implementation features:
  • Mock object creation for logger verification
  • Dynamic test generation using Ruby’s metaprogramming
  • Severity level iteration using %w array syntax
  • Expectation setting and verification using Minitest::Mock

Technical Details

Testing infrastructure includes:
  • Minitest framework with mocking support
  • RSpec-style describe/it blocks
  • test_helper integration
  • Mock expectation verification
  • Dynamic method invocation using send

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby.

Notable practices include:
  • Isolation of system under test using mocks
  • DRY principle application through test iteration
  • Clear test case organization
  • Explicit verification of expected behaviors
  • Comprehensive coverage of interface contracts

resque/resque

test/logging_test.rb

            
require 'test_helper'
require 'minitest/mock'

describe "Resque::Logging" do

  it "sets and receives the active logger" do
    my_logger = Object.new
    Resque.logger = my_logger
    assert_equal my_logger, Resque.logger
  end

  %w(debug info error fatal).each do |severity|
    it "logs #{severity} messages" do
      message       = "test message"
      mock_logger   = Minitest::Mock.new
      mock_logger.expect severity.to_sym, nil, [message]
      Resque.logger = mock_logger

      Resque::Logging.send severity, message
      mock_logger.verify
    end
  end
end