Blog

Friday, January 14, 2011

Stub called with unexpected arguments fixed in RSpec2 Mocks

In RSpec 1.3 the following would pass:

describe Person do
 describe "#greeting" do
   context "using #stub with correct parameters" do
     before(:each) do
       subject.stub(:greeting).with("Nicholas")
     end

     specify { lambda{ subject.greeting("Nicholas") }.should_not
raise_error(NoMethodError) }
   end

   context "using #stub with incorrect parameters" do
     before(:each) do
       subject.stub(:greeting).with("incorrect parameter")
     end
     specify { lambda{ subject.greeting("Nicholas") }.should
raise_error(NoMethodError) }
   end
 end
end

From the example above you can see that calling a stuff with a different parameter would raise a NoMethodError. A bit confusing.

Fortunately in RSpec 2 it fixed. The following error is raised:

1) Person#greeting using #stub with incorrect parameters 
     Failure/Error: specify { lambda{ subject.greeting("Nicholas") }.should raise_error(NoMethodError) }
       expected NoMethodError, got ##<RSpec::Mocks::MockExpectationError: #<Person:0x0000010216fb68#> received :greeting with unexpected arguments
         expected: ("incorrect parameter")
              got: ("Nicholas")>

Related links:

  • My post to RSpec mailing list
  • http://github.com/rspec/rspec-mocks/issues/20
  • http://groups.google.com/group/rspec/browse_thread/thread/ab3519f98597815c

Please note this blog is no longer maintained. Please visit CivilCode Inc - Custom Software Development.