stephenmurdoch
stephenmurdoch

Reputation: 34603

Rspec: Expecting a module to receive a method

I've got a really simple module:

module Foo
  def self.quux
    begin
      # stuff here
    ensure
      Baz.print_stuff
    end
  end
end

And then I have a simple test that fails:

describe Foo
  describe '.quux' do
    it "should print stuff" do
      Foo.quux
      expect(Baz).to receive(:print_stuff)
    end
  end
end

I get the following error message when I run this:

Failures:

1) Foo.quux should should print stuff
   Failure/Error: expect(Baz).to receive(:print_stuff)
     (Baz).print_stuff(any args)
         expected: 1 time with any arguments
         received: 0 times with any arguments

What am I doing wrong? I know for a fact that the print_stuff method is being called.

Upvotes: 3

Views: 2578

Answers (1)

stephenmurdoch
stephenmurdoch

Reputation: 34603

The solution:

describe Foo
  describe '.quux' do
    it "should print stuff" do
      expect(Baz).to receive(:print_stuff)
      Foo.quux
    end
  end
end

Put the expectation before the method call to Foo.quux.

Doh!

Upvotes: 6

Related Questions