spec/bogus/mocking_dsl_spec.rb in bogus-0.0.3 vs spec/bogus/mocking_dsl_spec.rb in bogus-0.0.4

- old
+ new

@@ -6,10 +6,13 @@ end def hello(greeting, name) end + def with_optional_args(x, y = 1) + end + def self.bar(baz) "Hello #{baz}" end end @@ -83,10 +86,16 @@ it "does not allow verifying on methods with a wrong argument count" do expect { the_fake.should Stubber.have_received.foo("test", "test 2") }.to raise_error(ArgumentError) end + + it "allows spying on methods with optional parameters" do + the_fake.with_optional_args(123) + + the_fake.should Stubber.have_received.with_optional_args(123) + end end it "can be used with plain old Ruby objects" do object = ExampleFoo.new Stubber.stub(object).foo(Stubber.any_args) @@ -94,10 +103,19 @@ object.foo('test') object.should Stubber.have_received.foo("test") end + it "allows spying on methods with optional parameters" do + object = ExampleFoo.new + Stubber.stub(object).with_optional_args(123) { 999 } + + object.with_optional_args(123).should == 999 + + object.should Stubber.have_received.with_optional_args(123) + end + class ClassWithMatches def matches?(something) end end @@ -134,30 +152,50 @@ describe "#mock" do let(:object) { ExampleFoo.new } let(:fake) { Bogus.fake_for(:example_foo) { ExampleFoo } } shared_examples_for "mocking dsl" do - before do - Mocker.mock(baz).foo("bar") { :return_value } + it "allows mocking on methods with optional parameters" do + Mocker.mock(baz).with_optional_args(1) { :return } + + baz.with_optional_args(1).should == :return + + expect { Bogus.after_each_test }.not_to raise_error end + it "allows mocking with anything" do + Mocker.mock(baz).hello(1, Bogus::Anything) { :return } + + baz.hello(1, 2).should == :return + + expect { Bogus.after_each_test }.not_to raise_error + end + it "allows mocking the existing methods" do + Mocker.mock(baz).foo("bar") { :return_value } + baz.foo("bar").should == :return_value + + expect { Bogus.after_each_test }.not_to raise_error end it "verifies that the methods mocked exist" do expect { Mocker.mock(baz).does_not_exist { "whatever" } }.to raise_error(NameError) end it "raises errors when mocks were not called" do + Mocker.mock(baz).foo("bar") + expect { Bogus.after_each_test }.to raise_error(Bogus::NotAllExpectationsSatisfied) end it "clears the data between tests" do + Mocker.mock(baz).foo("bar") + Bogus.send(:clear_expectations) expect { Bogus.after_each_test }.not_to raise_error(Bogus::NotAllExpectationsSatisfied)