Back to Repositories

Testing Sinatra Extension Configuration and Routing in Sinatra

This test suite validates the functionality of Sinatra::Extension module, which enables modular extension of Sinatra applications. It tests configuration settings, environment-specific behaviors, and route definitions within extensions.

Test Coverage Overview

The test suite provides comprehensive coverage of Sinatra::Extension’s core capabilities, focusing on three main areas: setting configuration values, environment-specific configuration, and route definition.

  • Tests setting application configuration values using ‘set’
  • Verifies environment-specific configuration behavior
  • Validates route definition and response handling
  • Covers extension registration and integration

Implementation Analysis

The testing approach utilizes RSpec’s describe blocks to organize tests around the Sinatra::Extension module functionality. It employs a mock application pattern to isolate extension behavior and verify its integration.

  • Uses RSpec’s expectation syntax for assertions
  • Implements before hooks for test setup
  • Employs mock_app helper for isolated testing
  • Demonstrates modular extension pattern

Technical Details

  • Testing Framework: RSpec
  • Test Environment: Configured for multiple environments (test, production, development)
  • Helper Methods: mock_app, settings, get
  • Configuration: Uses Sinatra’s configure blocks
  • Response Validation: Status codes and body content verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Sinatra extensions, including proper isolation of test cases and comprehensive feature coverage.

  • Isolated test environment setup
  • Clear test case organization
  • Environment-specific testing
  • Explicit expectations and assertions
  • Modular test structure

sinatra/sinatra

sinatra-contrib/spec/extension_spec.rb

            
require 'spec_helper'

RSpec.describe Sinatra::Extension do
  module ExampleExtension
    extend Sinatra::Extension

    set :foo, :bar
    settings.set :bar, :blah

    configure :test, :production do
      set :reload_stuff, false
    end

    configure :development do
      set :reload_stuff, true
    end

    get '/' do
      "from extension, yay"
    end
  end

  before { mock_app { register ExampleExtension }}

  it('allows using set') { expect(settings.foo).to eq(:bar) }
  it('implements configure') { expect(settings.reload_stuff).to be false }

  it 'allows defing routes' do
    expect(get('/')).to be_ok
    expect(body).to eq("from extension, yay")
  end
end