Skip to content

Instantly share code, notes, and snippets.

@JanOwiesniak

JanOwiesniak/ExpectTwice

Last active Dec 20, 2015
Embed
What would you like to do?
require 'minitest/autorun'
class ExpectTwice
def initialize(mock)
@mock = mock
end
def call
p :called_with_FOO => @mock.execute('FOO')
p :called_with_BAR => @mock.execute('BAR')
end
end
describe MiniTest::Mock do
subject { ExpectTwice.new(mock) }
let(:mock) { MiniTest::Mock.new }
it 'expects load twice' do
mock.expect :execute, :foo do |args|
assert_equal 1, args.size
assert_equal 'FOO', args[0]
end
mock.expect :execute, :bar do |args|
assert_equal 1, args.size
assert_equal 'BAR', args[0]
end
subject.call
mock.verify
end
end
@JanOwiesniak

This comment has been minimized.

Copy link
Owner Author

@JanOwiesniak JanOwiesniak commented Aug 6, 2013

require 'helper'

describe MyProject::EntityMapper::CateringPermission do

  subject           { MyProject::EntityMapper::CateringPermission.new(store) }
  let(:entity)      { FactoryGirl.build(:catering_permission, attributes) }
  let(:store)       { MiniTest::Mock.new }
  let(:attributes)  { FactoryGirl.attributes_for(:catering_permission) }

  it "inherits from Store::EntityMapper" do
    assert_kind_of Store::EntityMapper, subject
  end

  it "maps the entity" do
    store.expect :save, :ref_1 do |args|
      assert_equal 1, args.size
      assert_kind_of MyProject::Model::Catering, args[0]
    end

    store.expect :save, :ref_2 do |args|
      assert_equal 1, args.size
      assert_kind_of MyProject::Model::Participant, args[0]
    end

    expected = attributes.merge(:catering => :ref_1, :participant => :ref_2).stringify_keys

    assert_equal expected, subject.map(entity)
    store.verify
  end

  it "unmaps the entity" do
    catering = FactoryGirl.build(:catering)
    participant = FactoryGirl.build(:participant)

    mapped = attributes.merge(:catering => catering, :participant => participant).stringify_keys

    entity = FactoryGirl.build(:catering_permission, mapped)

    store.expect :load, catering do |args|
      assert_equal 1, args.size
      assert_equal mapped['catering'], args[0]
    end

    store.expect :load, participant do |args|
      assert_equal 1, args.size
      assert_equal mapped['participant'], args[0]
    end

    expected = entity

    assert_equal expected, subject.unmap(entity.class, mapped)
    store.verify
  end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.