[Rspec-devel] Mocha and TestCase#add_assertion

Jonathan del Strother maillist at steelskies.com
Tue Sep 12 05:24:13 EDT 2006

On 12 Sep 2006, at 09:57, James Mead wrote:

> I had a query on the Mocha mailing list this morning from someone  
> who is using RSpec and Mocha plugins. He was seeing an error which  
> looks to be down to RSpec raising an exception when add_assertion  
> is called. Mocha uses add_assertion to include verified  
> expectations in the assertion count. My colleague Chris Roos gives  
> the following tests as an illustration of the problem.
> # normal test, no errors
> def test_abc
>   add_assertion
> end
> # rspec spec, raises nil.add_assertion
> spec "should blah" do
>   add_assertion
> end
> I haven't looked into it any further, but does anyone have any  
> ideas? I suggested he patched his version of Mocha to rescue the  
> exception, but told him I wasn't sure of the ramifications on the  
> RSpec side.

Just FWIW, add_assertion expects a @_result object.  I needed to call  
assert_redirected_to, and was running into similar problems.  You  
could set it up before each call to assert (@_result =  
Test::Unit::TestResult.new), though this adds a dependency on  
Test:Unit which may or may not be desirable.  You could set up a fake  
object with an add_assertion method, and assign that to @_result.

...however, these are all hacks that are just there as a workaround,  
and I'm guessing there's a much better way of doing this.

More information about the rspec-devel mailing list