Sha256: 8fb93063cca10f2f63769ee1a4f35207785e0a8fcc842cb674b1f0a3e3317e6b

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require 'test_helper'

class TestContext
  using Casting::Context
  extend Casting::Context

  initialize :admin, :user

  def approve
    tell :admin, :say, 'I approve'
  end

  def approve_with_keyword
    tell :admin, :keyword_say, what: 'I approve'
  end

  def user_approve
    tell :user, :approve
  end

  module Admin
    def say(what)
      what
    end

    def keyword_say(what:)
      what
    end
  end

  module User
    def approve
      'Yay!'
    end
  end
end

class MissingModuleContext
  using Casting::Context
  extend Casting::Context

  initialize :admin, :user

  def run
    tell :admin, :go
  end
end

describe Casting::Context do
  it 'applies module methods to Casting::Client objects' do
    admin = casting_person
    user = casting_person

    context = TestContext.new admin: admin, user: user

    expect(context.approve).must_equal ('I approve')
    expect(context.approve_with_keyword).must_equal ('I approve')
    expect(context.user_approve).must_equal ('Yay!')
  end

  it 'applies module methods to any object' do
    admin = Object.new
    user = 1

    context = TestContext.new admin: admin, user: user

    expect(context.approve).must_equal ('I approve')
    expect(context.user_approve).must_equal ('Yay!')
  end

  it 'handles missing modules and raises missing method error' do
    admin = TestPerson.new
    user = TestPerson.new

    context = MissingModuleContext.new admin: admin, user: user

    err = expect{ context.run }.must_raise(NoMethodError)
    expect(err.message).must_match(/unknown method 'go'/)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
casting-1.0.1 test/context_test.rb
casting-1.0.0 test/context_test.rb