user1032752
user1032752

Reputation: 881

Minitest/Mocha: Testing that a value changes multiple times

Let's say I have a service class as such:

class FooService

  def self.execute(foo_id)
    Foo.find(foo_id).tap do |foo|
      foo.update_attribute :status, :working
      do_work(foo)
      foo.update_attribute :status, :done
    end
  end

end

A simple test for this method in Minitest with Mocha:

test 'executing the service' do
  @foo = Foo.first

  FooService.expects(:do_work).with(@foo)

  FooService.execute(@foo.id)

  assert_equal :done, @foo.reload.status
end

What would be the best way to test that the status attribute was set to :working?

I've tried using Foo.any_instance.expects(:update_attribute).with(:status, :working) but since there is no way to call the original implementation in Mocha this has bad side effects.

Upvotes: 0

Views: 195

Answers (1)

ReggieB
ReggieB

Reputation: 8257

One solution would be to get do_work to raise an error. That should stop the process before the end of the routine and leave foo in status working

FooService.expects(:do_work).raises(Exception, 'foo')
assert_equal :working, @foo.reload.status

Upvotes: 0

Related Questions