[mocha-developer] Using any_instance with Rails to make sure the right thing is saved?

James Moore jamesthepiper at gmail.com
Wed Feb 7 15:31:34 EST 2007

On 2/6/07, James Mead <jamesmead44 at gmail.com> wrote:
> So you'd have something along these lines in your test...
>   annotation = mock('annotation')
>   annotation.expects(:update_attributes).with(:name =>
> 'wibble').returns(true)

Is there a standard way to get information from the 'with' bit to the
'return' bit?  It'd be useful to be able to build the return object based on

information from the original call to 'new'.  I did it by stashing away the
values in the 'with' block to be used later on in the Proc used by return():

    # Set up S2TestSuite so it creates the
    # mocks we've just defined
    newargs = nil
    mck = S2TestSuite.stubs(:new).with do |*args|
      newargs = HashWithIndifferentAccess.new.merge!(args.first)
    objs = [foosuite, barsuite]
    generator = lambda do
      result = objs.shift
      result.attributes = newargs

      # #new handles arguments differently; it'll
      # take an id and turn it into an object.
      # #attributes= doesn't do that.
      if i = newargs[:s2_test_path_id]
        result.s2_test_path = S2TestPath.find(i.to_i)
    mck.returns generator

But that seems goofy.  Is there a) either a better way to pass around the
values from #with to #returns, or b) am I just misusing mocks, and
I should always specify all the values?

 - James Moore

More information about the mocha-developer mailing list