Skip to content

Instantly share code, notes, and snippets.

@psyho
Created April 29, 2014 15:08
Show Gist options
  • Save psyho/11403143 to your computer and use it in GitHub Desktop.
Save psyho/11403143 to your computer and use it in GitHub Desktop.
How to combine Bogus and rspec-mocks (or other mocking library) in one project
require 'spec_helper'
class Foo
def initialize(bar)
@bar = bar
end
def foo
@bar.bar
end
end
class Bar
def bar
end
end
describe "tests that use rspec-expectations" do
describe "using new syntax" do
it "should allow mocking" do
bar = mock()
foo = Foo.new(bar)
bar.should_receive(:bar).and_return(123)
foo.foo.should == 123
end
it "should allow spying" do
bar = stub(bar: 123)
foo = Foo.new(bar)
foo.foo
bar.should have_received(:bar)
end
it "should fail because of an unsatisfied expectation" do
bar = mock()
bar.should_receive(:bar).and_return(123)
end
end
describe "using old syntax" do
it "should allow mocking" do
bar = double()
foo = Foo.new(bar)
expect(bar).to receive(:bar).and_return(123)
expect(foo.foo).to eq(123)
end
it "should allow spying" do
bar = double(bar: 123)
foo = Foo.new(bar)
foo.foo
expect(bar).to have_received(:bar)
end
it "should fail because of an unsatisfied expectation" do
bar = double()
expect(bar).to receive(:bar).and_return(123)
end
end
end
describe "tests that use bogus", :bogus do
fake(:bar)
it "allows spying using bogus syntax" do
foo = Foo.new(bar)
foo.foo
expect(bar).to have_received.bar
end
it "allows stubbing" do
foo = Foo.new(bar)
stub(bar).bar { 123 }
expect(foo.foo).to eq(123)
end
it "fails because of unsatisfied expectations" do
mock(bar).bar { 123 }
end
end
require 'bogus'
RSpec.configure do |config|
# this is only so that we can write:
# describe Foo, :bogus do
# instead of
# describe Foo, bogus: true do
config.treat_symbols_as_metadata_keys_with_true_values = true
config.mock_with :rspec
# unfortunately it's not enough to write:
# config.include Bogus::MockingDSL, :bogus
# because the methods from Rspec::Mocks::ExampleMethods will have
# preference then
config.before(:each, :bogus) do
extend Bogus::MockingDSL
end
config.after(:each, :bogus) do
Bogus.after_each_test
end
config.after(:suite) do
Bogus.reset!
end
config.extend Bogus::RSpecExtensions, :bogus
# just use the version that matches your RSpec
if RSpec::Core::Version::STRING >= "2.14"
config.backtrace_exclusion_patterns << Regexp.new("lib/bogus")
else
config.backtrace_clean_patterns << Regexp.new("lib/bogus")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment