RSpec mock question

How can I mock the XMLRPC::Client.new3 class method using rspec’s mock?
I expected this spec:

    describe Foo do
      it 'should call new3' do
        options = {}
        XMLRPC::Client.any_instance.should_receive(:new3).with(options)
        Foo.new(options)
      end
    end

with this class definition:

    class Foo
      def new(options)
        @server = XMLRPC::Client.new3(options)
      end
    end

to pass but instead I got the error:

      1) Foo should call new 3
         Failure/Error: Unable to find matching line from backtrace
           Exactly one instance should have received the following

message(s) but didn’t: new3

How can I change the spec to make this pass (while verifying that new3
was called)?

Thanks,
Christopher

      1) Foo should call new 3
         Failure/Error: Unable to find matching line from backtrace
           Exactly one instance should have received the following

message(s) but didn’t: new3

How can I change the spec to make this pass (while verifying that new3
was called)?

This probably isn’t working because .new3 is a class method, and you are
setting an expectation for an instance method to be called. The way I
would do this would be something like:

client = stub(“XMLRPC::Client”, :new3 => true)
XMLPRC::Client.should_receive(:new3).with(options).and_return(client)

Patrick J. Collins
http://collinatorstudios.com

I knew it was a class method but wasn’t sure how to put an expectation
on a class method. I tried your suggestion but, unfortunately, it still
fails, although with a different error message:

  1) Foo should call new 3
     Failure/Error:
XMLRPC::Client.should_receive(:new3).with(options).and_return(client)
       (<XMLRPC::Client (class)>).new3({:host=>"host.com",
:path=>"xmlrpc.php", :port=>"80", :user=>"username",
:password=>"password"})
           expected: 1 time
           received: 0 times

Any other suggestions?

Thanks for your help,
Christopher

I got it working. I had defined new on Foo rather than initialize so it
was my dumb mistake that prevented your solution from working. Thanks a
lot for your help Patrick.

Christopher