Monday, December 27, 2010

[Rails] 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

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate