Yi Wen
2008-Sep-08 17:12 UTC
[rspec-users] How to unit test code that connects to the external resources?
By external resources, I mean, the code start a http connection and GET xmls from the url specified. I will definitely not rely my unit test on an external url or anything like that. But how do I unit test the method? I mean, I can basically mock the Net::HTTP, but then the test will pretty much be like the method itself, except all the calls will be replaced by stub! or should_receive. I made this method as simple as possible. There is no *if* statements or loop in the method, should I just NOT test this one? Thanks for any ideas Yi -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080908/ae51bae1/attachment.html>
Chuck Remes
2008-Sep-08 20:23 UTC
[rspec-users] How to unit test code that connects to the external resources?
On Sep 8, 2008, at 12:12 PM, Yi Wen wrote:> By external resources, I mean, the code start a http connection and > GET xmls from the url specified. I will definitely not rely my unit > test on an external url or anything like that. But how do I unit > test the method? I mean, I can basically mock the Net::HTTP, but > then the test will pretty much be like the method itself, except all > the calls will be replaced by stub! or should_receive. > > I made this method as simple as possible. There is no if statements > or loop in the method, should I just NOT test this one? > > Thanks for any ideasShow us the code so we can help. cr -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080908/0d50dfb0/attachment.html>
Yi Wen
2008-Sep-08 20:29 UTC
[rspec-users] How to unit test code that connects to the external resources?
def run
url = URI.parse(run_path)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = use_ssl_for_run
http.start {
req = Net::HTTP::Get.new run_path
req.basic_auth self.class.user, self.class.password
@response = http.request(req)
}
end
run_path and use_ssl_for_run are both instance methods. run
On Mon, Sep 8, 2008 at 3:23 PM, Chuck Remes <cremes.devlist at mac.com>
wrote:
>
> On Sep 8, 2008, at 12:12 PM, Yi Wen wrote:
>
> By external resources, I mean, the code start a http connection and GET
> xmls from the url specified. I will definitely not rely my unit test on an
> external url or anything like that. But how do I unit test the method? I
> mean, I can basically mock the Net::HTTP, but then the test will pretty
much
> be like the method itself, except all the calls will be replaced by stub!
or
> should_receive.
>
> I made this method as simple as possible. There is no *if* statements or
> loop in the method, should I just NOT test this one?
>
> Thanks for any ideas
>
>
> Show us the code so we can help.
>
> cr
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20080908/3280d4d0/attachment-0001.html>
Josh Knowles
2008-Sep-08 20:51 UTC
[rspec-users] How to unit test code that connects to the external resources?
On 9/8/08, Yi Wen <hayafirst at gmail.com> wrote:> By external resources, I mean, the code start a http connection and GET xmls > from the url specified.Take a look at the video from Ruby Hoedown in which Joe O''brien and Jim Weirich dicuss this topic: http://rubyhoedown2008.confreaks.com/02-joe-obrien-and-jim-weirich-mock-dialogue.html -- Josh Knowles phone: 509-979-1593 email: joshknowles at gmail.com web: http://joshknowles.com