Module: On::TestHelper

Defined in:
lib/on/test_helper.rb

Overview

Helper for testing On callbacks.

Example

require 'minitest/autorun'
require 'on'
require 'on/test_helper'

class SomeTest < MiniTest::Spec
  include On::TestHelper

  let(:recorder) { On::TestHelper::Recorder.new }

  it "records everything" do
    on = On.new(:success, :failure, &recorder)
    on.call :success, :some, :args
    assert_callback recorder, :success, :some, :args
  end

  it "calls nothing" do
    on = On.new(:success, :failure, &recorder)
    # nothing called
    refute_callbacks recorder
  end

  it "records everything manually" do
    on = On.new(:success, :failure) do |result|
      recorder.record_block
      recorder.record_callback(result, :success, :failure)
    end
    on.call :success, :some, :args
    assert_callback recorder, :success, :some, :args
  end

  it "checks record args in a block" do
    on = On.new(:success, :failure) do |result|
      recorder.record_block
      recorder.record_callback(result, :success, :failure)
    end
    on.call :success, :some, :args
    assert_callback recorder, :success do |some, args|
      assert_equal :some, some
      assert_equal :args, args
    end
  end
end

Defined Under Namespace

Classes: Recorder

Instance Method Summary collapse

Instance Method Details

#assert_callback(recorder, name, *args) ⇒ Object

Asserts that a certain callbacks has been recorded by recorder.

Example

assert_callback recorder, :success, "some", "args"

Raises:

  • (ArgumentError)

55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/on/test_helper.rb', line 55

def assert_callback(recorder, name, *args)
  raise ArgumentError, "Provide args or block but not both" if block_given? && !args.empty?

  callback = recorder.last_record
  assertion_message = proc { "Callback was #{callback}" }
  assert callback, "No callbacks found"
  assert_equal name, callback.name, assertion_message
  if block_given?
    yield *callback.args
  else
    assert_equal args, callback.args, assertion_message
  end
end

#refute_callbacks(recorder) ⇒ Object

Asserts that no callbacks has been recorder by recorder.


70
71
72
# File 'lib/on/test_helper.rb', line 70

def refute_callbacks(recorder)
  assert recorder.empty?, "Something has been recorded #{recorder.inspect}"
end