[mocha-developer] yielding consecutive values

James Mead jamesmead44 at gmail.com
Fri Oct 6 05:13:10 EDT 2006


On 29/09/06, Jay <jay at jayfields.com> wrote:
>
> I'm not sure if someone has already addressed this issue, but if not,
> here goes.
>
> I need the following behavior:
>    def test_generate(documentation = '/test_documentation/',
> destination = '/destination/')
>      yield_results = ['/', 'filename.yaml', 'blah.xml']
>      Find.expects(:find).with(documentation).yields(lambda
> { yield_results.shift } )
>      ...
>    end
>
> So I made these changes...
>
> Index: test/mocha/expectation_test.rb
> ===================================================================
> --- test/mocha/expectation_test.rb      (revision 62)
> +++ test/mocha/expectation_test.rb      (working copy)
> @@ -91,6 +91,11 @@
>       assert_equal parameters_for_yield, yielded_parameters
>     end
> +  def test_should_yield_with_block_result
> +    expectation = new_expectation.yields( lambda { 'lambda return
> value'  } )
> +    expectation.invoke() { |*parameters| assert_equal ['lambda
> return value'], parameters }
> +  end
> +
>     def test_should_return_specified_value
>       expectation = new_expectation.returns(99)
>       assert_equal 99, expectation.invoke
> Index: lib/mocha/expectation.rb
> ===================================================================
> --- lib/mocha/expectation.rb    (revision 62)
> +++ lib/mocha/expectation.rb    (working copy)
> @@ -217,7 +217,12 @@
>
>       def invoke
>         @invoked += 1
> -      yield(*@parameters_to_yield) if yield? and block_given?
> +      if yield? and block_given?
> +        params = @parameters_to_yield.collect do |element|
> +          element.is_a?(Proc) ? element.call : element
> +        end
> +        yield(*params)
> +      end
>         @return_value.is_a?(Proc) ? @return_value.call : @return_value
>       end
>
> This test passes also, but I didn't think it was necessary for the
> suite...
>    def test_should_yield_with_block_result_1123
>      expected_vals = [1,2,3]
>      expectation = new_expectation.yields( lambda
> { expected_vals.shift }, 4 )
>      expectation.invoke() { |*parameters| assert_equal [1, 4],
> parameters }
>      expectation.invoke() { |*parameters| assert_equal [2, 4],
> parameters }
>      expectation.invoke() { |*parameters| assert_equal [3, 4],
> parameters }
>    end
>
>
> Along the same thread, I thought about using a comma separated list
> (similar to the new returns format); however, it wont work since the
> parameters are captured as args and returned as one value.
>
> Thoughts?


Thanks for your patch. I'm sorry not to have replied sooner - works been
busy and then I was off sick.

I can see the need for yields() to accept a Proc in the same way that
returns() does. However, I'm not totally convinced that accepting multiple
Procs actually gains us anything - could you not rewrite your example test
like this...

   def test_should_yield_with_block_result_1123
     expected_vals = [1,2,3]
     expectation = new_expectation.yields( lambda { [expected_vals.shift, 4]
} )
     expectation.invoke() { |*parameters| assert_equal [1, 4], parameters }
     expectation.invoke() { |*parameters| assert_equal [2, 4], parameters }
     expectation.invoke() { |*parameters| assert_equal [3, 4], parameters }
   end

I'm trying to decide which is the most consistent / unsurprising approach.

What do you think?
-- 
James.
http://blog.floehopper.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20061006/05414e63/attachment-0001.html 


More information about the mocha-developer mailing list