Sha256: e5e876764e3e285147e4f05ab54e5eb79d22ea6d2f7ffdadc5d0377e3ec338f3

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

require 'test_helper'
require 'casting/client'

describe Casting::MissingMethodClient, '#cast_as' do
  def client
    @client ||= test_person.extend(Casting::Client, Casting::MissingMethodClient)
  end

  it "sets the object's delegate for missing methods" do
    client.cast_as(TestPerson::Greeter)
    assert_equal 'hello', client.greet
  end

  it "delegates to objects of the same type" do
    client.extend(TestPerson::Greeter)
    attendant = client.clone
    client.singleton_class.send(:undef_method, :greet)
    client.cast_as(attendant)
    assert_equal 'hello', client.greet
  end

  it "returns the object for further operation" do
    jim = test_person.extend(Casting::Client, Casting::MissingMethodClient)

    assert_equal 'hello', jim.cast_as(TestPerson::Greeter).greet
  end
end

describe Casting::MissingMethodClient, '#uncast' do
  def client
    @client ||= test_person.extend(Casting::Client, Casting::MissingMethodClient)
  end

  it "removes the last added delegate" do
    client.cast_as(TestPerson::Greeter)
    assert_equal 'hello', client.greet
    client.uncast
    assert_raises(NoMethodError){ client.greet }
  end

  it "maintains any previously added delegates" do
    client.cast_as(TestPerson::Verbose)
    assert_equal 'one,two', client.verbose('one', 'two')
    client.uncast
    assert_raises(NoMethodError){ client.verbose('one', 'two') }
  end

  it "returns the object for further operation" do
    jim = test_person.extend(Casting::Client, Casting::MissingMethodClient)

    assert_equal 'name from TestPerson', jim.uncast.name
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
casting-0.6.1 test/missing_method_client_test.rb
casting-0.5.2 test/missing_method_client_test.rb