On 7/10/07, Ashley Moran <[EMAIL PROTECTED]> wrote: > Hi > > Just wrote myself a Date.extract_from_rails_hash to handle parsing > the "date(1i)", "date(2i)" parameters created in the controller > params. I've got a method that needs to call this either once or > twice, depending on the contents of the form (one section of the form > is rendered conditionally). So I had two specs for the case where > the second form section is included: > > it "..." do > Date.should_receive(:extract_from_rails_hash). > with(REQUEST_ARGS_WITH_FINANCE_AGREEMENT > [:vehicle], :registration_date). > and_return(Date.new(2006, 12, 1)) > # ... > end > > it "..." do > Date.should_receive(:extract_from_rails_hash). > with(REQUEST_ARGS_WITH_FINANCE_AGREEMENT > [:finance_agreement], :start_date). > and_return(Date.new(2006, 12, 4)) > # ... > end > > In each of these specs I only care about one of the calls, but they > fail because the Date method is being called twice, and it reports > the other call as an error. > > What is the thinking behind this? I know you shouldn't have > unspecified code, but it seems restrictive to force all calls to a > method to be covered in the same example.
This is pretty standard in mocking frameworks. In fact, your frustration was Jim Weirich's motivation for writing Flexmock, so you may want to give that framework a shot. Using RSpec's mocks, the way I get around this is to stub the call in before(:each) and the mock the call in the example: describe "something" do before(:each) do Date.stub!(:extract_from_rails_hash) end it "..." do Date.should_receive(:extract_from_rails_hash). with(one_set_of_args). and_return(Date.new(2006, 12, 1)) # ... end it "..." do Date.should_receive(:extract_from_rails_hash). with(another_set_of_args). and_return(Date.new(2006, 12, 1)) # ... end end This tells the Date class object to ignore all extract_from_rails_hash messages except the one that you are interested in in th example. David > > Thanks > Ashley > > _______________________________________________ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users