Sha256: 304dcf41b87be2b0e3fd817e8b76c64fb8d872ff7c66820badc30ba1d2f90f68

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

require "spec_helper"

describe MethodCallbacks do
  let(:test_callbacks) { TestCallbacks.new }

  it "should execute all the callbacks on action" do
    expect(test_callbacks).to receive(:puts).with("Executing block").ordered
    expect(test_callbacks).to receive(:puts).with("Executing intro").ordered
    expect(test_callbacks).to receive(:puts).with("Executing greet").ordered
    expect(test_callbacks).to receive(:puts).with("Executing pre outer_around").ordered
    expect(test_callbacks).to receive(:puts).with("Executing pre inner_around").ordered
    expect(test_callbacks).to receive(:puts).with("Executing action").ordered
    expect(test_callbacks).to receive(:puts).with("Executing post inner_around").ordered
    expect(test_callbacks).to receive(:puts).with("Executing post outer_around").ordered
    expect(test_callbacks).to receive(:puts).with("Executing farewell").ordered
    expect(test_callbacks).to receive(:puts).with("Executing unload").ordered
    expect(test_callbacks).to receive(:puts).with("Executing block").ordered

    expect(test_callbacks.action).to eq("Return value")
  end
end

class TestCallbacks
  include MethodCallbacks

  def intro
    puts "Executing intro"
  end

  def greet
    puts "Executing greet"
  end

  before_method :action do
    puts "Executing block"
  end
  before_method :action, :intro
  before_method :action, :greet
  def action
    puts "Executing action"

    "Return value"
  end
  after_method :action, :farewell, :unload
  after_method :action do
    puts "Executing block"
  end

  def farewell
    puts "Executing farewell"
  end

  def unload
    puts "Executing unload"
  end

  def outer_around
    puts "Executing pre outer_around"
    return_value = yield
    puts "Executing post outer_around"
    return_value
  end

  around_method :action, :outer_around, :inner_around

  def inner_around
    puts "Executing pre inner_around"
    return_value = yield
    puts "Executing post inner_around"
    return_value
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
method_callbacks-1.1.1 spec/method_callbacks_spec.rb
method_callbacks-1.1.0 spec/method_callbacks_spec.rb