[mocha-developer] yielding consecutive values

Jay jay at jayfields.com
Fri Sep 29 10:47:59 EDT 2006


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?


More information about the mocha-developer mailing list