Using Mocha to stub value assignments

Hi
I’m using Mocha to mock a HTTP connection, so I can test higher level
application code without requiring a ‘live’ webserver host. However, I’m
having difficulty trying to stub some of the
connection handle parameters.

My connection class init opens the port as follows:
class NetConnection
def initialize(host, port)
# – Outgoing is an Https post
@handle_tx = Net::HTTP.new(host, port)
@handle_tx.use_ssl = true
@handle_tx.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end

In my test class, I’m mocking the Net::HTTP connection as follows:

def test_open_port
net_mock = mock(‘Net::HTTP’)
Net::HTTP.expects(:new).once.with(exp_host,
exp_port).returns(net_mock)

  net_connection = NetConnection.new(exp_host, exp_port)

end

The problem is in the following calls in the NetConnection constructor
@handle_tx.use_ssl = true
@handle_tx.verify_mode = OpenSSL::SSL::VERIFY_NONE

Since these are assignments, not methods with parameters, how do I stub
them? I couldn’t find an appropriate method in the Mocha API

I tried the following, but they don’t ‘match’ the actual call
net_mock.expects(:use_ssl).once.with(true)
net_mock.expects(:verify_mode).once.with(‘OpenSSL::SSL::VERIFY_NONE’)

I get the following error when I use the above expects for :use_ssl

  1. Failure:
    unexpected invocation: #Mock:Net::HTTP.use_ssl=(true)
    unsatisfied expectations:
  • expected exactly once, not yet invoked:
    #Mock:Net::HTTP.use_ssl(true)
    satisfied expectations:
  • expected exactly once, already invoked once: - expected exactly once,
    already invoked once: Net::HTTP.new(exp_host, exp_port)

Any suggestions?

thanks
Michael

Michael C. wrote in post #970988:

Hi
I’m using Mocha to mock a HTTP connection, so I can test higher level
application code without requiring a ‘live’ webserver host. However, I’m
having difficulty trying to stub some of the
connection handle parameters.

This doesn’t really have anything to do with Rails, so it should have
been asked on the main Ruby list…but why not use WebMock? It already
does exactly what you’re trying to do.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

On Tue, Dec 28, 2010 at 8:15 AM, Marnen Laibow-Koser
[email protected]wrote:

Michael C. wrote in post #970988:

Hi
I’m using Mocha to mock a HTTP connection, so I can test higher level
application code without requiring a ‘live’ webserver host. However, I’m
having difficulty trying to stub some of the
connection handle parameters.

This doesn’t really have anything to do with Rails, so it should have
been asked on the main Ruby list…

OT, but what is the ‘main’ ruby list? I just looked for google groups
for
‘ruby’ and see a few, any recommendations?

David K. wrote in post #971102:

On Tue, Dec 28, 2010 at 8:15 AM, Marnen Laibow-Koser
[email protected]wrote:

Michael C. wrote in post #970988:

Hi
I’m using Mocha to mock a HTTP connection, so I can test higher level
application code without requiring a ‘live’ webserver host. However, I’m
having difficulty trying to stub some of the
connection handle parameters.

This doesn’t really have anything to do with Rails, so it should have
been asked on the main Ruby list…

OT, but what is the ‘main’ ruby list? I just looked for google groups
for
‘ruby’ and see a few, any recommendations?

I believe the main list is called ruby-talk. (I use ruby-forum.com for
my Web interface, though, not Google G…)

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

On Dec 28, 1:07am, Michael C. [email protected] wrote:

The problem is in the following calls in the NetConnection constructor
@handle_tx.use_ssl = true
@handle_tx.verify_mode = OpenSSL::SSL::VERIFY_NONE

Since these are assignments, not methods with parameters, how do I stub
them? I couldn’t find an appropriate method in the Mocha API

These are method calls (and the corresponding method name is foo= )

Fred

Frederick C. wrote in post #971106:

These are method calls (and the corresponding method name is foo= )

Fred

Fred
That worked - thank you. I figured it was something simple that I was
missing

Marnen/David
I apologize if this wasn’t the correct list to post this question. The
[email protected] forum seems to be ‘dead’, and when I did a search
across all forums at www.ruby-forum.com for previous posts about
‘mocha’, this one gave me the most hits so I thought I’d start here

thanks everyone
Michael