psyho / bogus

Fake library for Ruby
Other
359 stars 14 forks source link

Using test spies in bogus #38

Closed harlow closed 11 years ago

harlow commented 11 years ago

I'm trying to compare using bogus to rspec-mocks and I've run into an issue with test spies:

require 'spec_helper'

class PushNotifier
  def notify_async(messages)
  end
end

class CommentAdder
  def initialize(notifier)
    @notifier = notifier
  end

  def add(comment)
    # do some database stuff here...
    @notifier.notify_async("Comment: '#{comment}' added.")
  end
end

describe CommentAdder, '#add' do
  context 'bogus' do
    it "send push notifications when comment is added" do
      push_notifier = fake(:push_notifier, notify_async: true)

      CommentAdder.new(push_notifier).add("Hello world!")

      expect(push_notifier).to have_received(:notify_async).with("Comment: 'Hello world!' added.")
    end
  end

  context 'rspec-mocks' do
    it "send push notifications when comment is added" do
      push_notifier = double(notify_async: true)

      CommentAdder.new(push_notifier).add("Hello world!")

      expect(push_notifier).to have_received(:notify_async).with("Comment: 'Hello world!' added.")
    end
  end
end

Then the console output from RSpec looks like this:

:!rspec spec/bogus_spies_spec.rb
F.

Failures:

  1) CommentAdder#add bogus send push notifications when comment is added
     Failure/Error: expect(push_notifier).to have_received(:notify_async).with("Comment: 'Hello world!' added.")
       #<PushNotifier:0x3fd569f46cfc> expected to have received notify_async, but that method has not been stubbed.
     # ./spec/bogus_spies_spec.rb:26:in `block (3 levels) in <top (required)>'

Finished in 0.00737 seconds
2 examples, 1 failure

Failed examples:

rspec ./spec/bogus_spies_spec.rb:21 # CommentAdder#add bogus send push notifications when comment is added

shell returned 1  

Is there something simple I'm missing with the syntax here?

wrozka commented 11 years ago

Looks like you are using two mocking libraries at the same time. There is a conflict in the have_received matcher, probably you are using the one from rspec-mocks. You should have separate test suites for rspec-mocks and bogus if you want to compare them.

harlow commented 11 years ago

@wrozka thanks for the quick reply. that makes total sense.