Back to Repositories

Testing Searchkick Notification System Implementation in ankane/searchkick

This test suite validates the notification system functionality in Searchkick, focusing on search operation events and their proper capture. It ensures that search operations trigger appropriate notifications and verifies the integration between Searchkick and ActiveSupport::Notifications.

Test Coverage Overview

The test suite provides coverage for Searchkick’s notification system, specifically focusing on search operations.

Key areas tested include:
  • Search operation notification emission
  • Notification payload structure
  • Integration with ActiveSupport::Notifications
  • Proper notification capture and handling

Implementation Analysis

The testing approach employs a helper method ‘capture_notifications’ that creates a controlled environment for notification capture.

Technical implementation features:
  • Lambda callback for notification handling
  • Subscription to Searchkick-specific notifications
  • Notification data structure validation
  • Index refresh before testing

Technical Details

Testing infrastructure includes:
  • Minitest framework usage
  • ActiveSupport::Notifications integration
  • Custom notification capture mechanism
  • Product model with Searchkick integration
  • Index management utilities

Best Practices Demonstrated

The test demonstrates several testing best practices in Ruby.

Notable practices include:
  • Isolated test environment setup
  • Clear separation of test and helper methods
  • Explicit assertion checks
  • Clean notification capture implementation
  • Proper test data management

ankane/searchkick

test/notifications_test.rb

            
require_relative "test_helper"

class NotificationsTest < Minitest::Test
  def test_search
    Product.searchkick_index.refresh

    notifications = capture_notifications do
      Product.search("product").to_a
    end

    assert_equal 1, notifications.size
    assert_equal "search.searchkick", notifications.last[:name]
  end

  private

  def capture_notifications
    notifications = []
    callback = lambda do |name, started, finished, unique_id, payload|
      notifications << {name: name, payload: payload}
    end
    ActiveSupport::Notifications.subscribed(callback, /searchkick/) do
      yield
    end
    notifications
  end
end