From: Enrique Comba Riepenhausen Date: 2007-05-26T04:25:59+09:00 Subject: Re: Mocking Net::HTTP --Apple-Mail-3--49510054 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On 25 May 2007, at 20:09, James Mead wrote: > On 25/05/07, Enrique Comba Riepenhausen wrote: >> What would you recommend? Should I write a MockGet/Post adapter that >> will do the job? Or is there a transparent way to change the behavior >> of Net::HTTP without touching the adapters? > > You should be able to use Mocha (http://mocha.rubyforge.org) do do > this. > > Let me know if you have a specific test you need help with. > -- > James. > http://blog.floehopper.org > Hi James, Brian! I've taken a look at both frameworks and the following question arises. If I am using my classes like the way I am using, how can I possibly mockup Net::HTTP? Let me try to explain with a sample testing code (taken from one of the unit tests): def test_send_through_get_adapter Rannel::Base.establish_connection(@adapters["get"]) # The configuration comes from Yaml sms = Rannel::Base.create_sms sms.from = "123124" sms.to = 2356556652 sms.text = "Hello" assert_equal(@OK, Rannel::Base.deliver(sms)) end The problem here is that the deliver(sms) call actually calls the adapter to deliver the SMS. In the case of the GetRequestAdapter (the one that makes the GET request) this looks like this: def deliver(sms) result = "" Net::HTTP.start(@ip, @port) do |http| url = "/cgi-bin/sendsms?username=#{@user}" + "&password=#{@password}#{sms.to_kannel}" response = http.get(url) result = response.body end result end So how can I then, from the Unit Test mockup the call that is being made to Net::HTTP? Thanks a lot for the help! ---- Enrique Comba Riepenhausen ecomba@mac.com I always thought Smalltalk would beat Java, I just didn't know it would be called 'Ruby' when it did. -- Kent Beck --Apple-Mail-3--49510054--