[MacRuby-devel] macruby rspec mock odd behavior

Gabriel Ayuso gabriel at gabrielayuso.com
Fri May 6 10:00:25 PDT 2011


Hi Josh,

Thanks for your feedback. After calling instance_methods on my class I saw that the selector was called and also tried to setup the expectation as you mentioned. Sadly this didn't work either. I get the following syntax error:

...FFF

Failures:

  1) MyClass loginWithEmail:password: should call :loginSucceededWithInfo after successful authentication
     Failure/Error: @requestBuilder.should_receive(:"buildRequestWithUrl:method:header:body:")
     SyntaxError:
       /Library/Frameworks/MacRuby.framework/Versions/0.10/usr/lib/ruby/Gems/1.9.2/gems/rspec-mocks-2.5.0/lib/rspec/mocks/method_double.rb:78: syntax error, unexpected ':', expecting ';' or '\n'
                 def buildRequestWithUrl:method:header:body:(*args, &block)
                                         ^
       /Library/Frameworks/MacRuby.framework/Versions/0.10/usr/lib/ruby/Gems/1.9.2/gems/rspec-mocks-2.5.0/lib/rspec/mocks/method_double.rb:78: syntax error, unexpected tAMPER
                 def buildRequestWithUrl:method:header:body:(*args, &block)
                                                                     ^
       /Library/Frameworks/MacRuby.framework/Versions/0.10/usr/lib/ruby/Gems/1.9.2/gems/rspec-mocks-2.5.0/lib/rspec/mocks/method_double.rb:79: syntax error, unexpected ':', expecting $end
       ...received :buildRequestWithUrl:method:header:body:, *args, &b...
       ...                              ^
     # ./Tumblizr/spec/Model/GATumblrAuthenticator_spec.rb:54:in `block'

Curious enough, in another project of mine I'm also mocking the same method but in that case it worked.

This expectation was setup with Mocha, it's testing a macruby class not an Objective C class as I'm trying to do in the other project.

request_builder.expects( :buildRequestWithUrl )
        .with() do |url, args|
            
            assert( ( url.include?( users[0] ) && 
                      url.include?( users[1] ) && 
                      url.include?( api_key  ) ), "Invalid URL." )
            
            assert_equal( "GET", args[:method], "Wrong HTTP method." )
        end

In the other project I also tried to use mocha instead of rspec's mocks but I had the same issues. Apparently the mocking frameworks can't define a method with the selector sent to the mock.

Gabriel Ayuso

On May 6, 2011, at 4:26 PM, Joshua Ballanco wrote:

> Hi Gabriel,
> 
> So, you've stumbled across a bit of a confusing "feature" of MacRuby and interaction with Obj-C. The full method name of the Obj-C method, as Ruby sees it, is actually "buildRequestWithUrl:method:header:body:", since this is the selector. So, I think your mock should work if you simply modify it as follows:
> 
> @requestBuilder.should_receive(:"buildRequestWithUrl:method:header:body:")
> .with("http://example.com", method: "POST", header: nil, body: anything() )
> .and_return(nil)
> 
> Please let us know if that doesn't work!
> 
> Cheers,
> 
> Josh
> On Wednesday, May 4, 2011 at 4:16 PM, Gabriel Ayuso wrote: 
>> Hi,
>> 
>> I'm having some strange issue with rspec mocks and hope someone can point out what I'm doing wrong.
>> 
>> I have a Objective C class with the method: buildRequestWithUrl:method:header:body:
>> I wrote an rspec mock for this class. I'm setting up an expectation as follows:
>> 
>> @requestBuilder.should_receive(:buildRequestWithUrl)
>> .with("http://example.com", method:"POST", header:nil, body:anything() )
>> .and_return(nil)
>> 
>> 
>> When the class being tested calls the aforementioned method I get the following error:
>> 
>> RuntimeError:
>> NSInvalidArgumentException: -[Mock buildRequestWithUrl:method:header:body:]: unrecognized selector sent to instance 0x200373ea0
>> 
>> I set the expectations for another method of the same class called :queryStringForDictionary
>> In this case the expectation and call do work. 
>> 
>> @requestBuilder.should_receive(:queryStringForDictionary).and_return( body )
>> 
>> In another project I'm calling the same class from ruby code and there the mocks and expectations do work properly. In this Objective C project they don't. 
>> 
>> Thanks in advance,
>> Gabriel Ayuso
>> 
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20110506/060c2f14/attachment.html>


More information about the MacRuby-devel mailing list