Hi I''m building a small library to use in my Rails app which interacts with a 3rd party API. It fetches artists and tracks. So I have: lib/my_lib.rb lib/my_lib/artist.rb lib/my_lib/track.rb To get an artist I do artist = MyLib::Artist.find("Oasis") Which returns an instance of Artist if it finds (via 3rd party API) it or nil otherwise. Then I can call artist.tracks which fetches all available tracks at the 3rd party and returns them as an array. In my artist class I have a separate method that makes the actual API calls and parses the XML response with Nokogiri def self.find(name: nil) # Stuff... doc = get_data(some_url) # More stuff... end def get_data(req_url) Nokogiri::XML(open(req_url)) end When writing my tests I don''t want to hit the actual URL but instead load an XML file from disk. I''m using Minitest which has a mocking library, but how do I actually mock (or is it more appropriate to stub?) the method? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a2e63640-ac9a-4114-a949-7a8caaab7468%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Hello man, I think you can easily accomplish this task declaring a variable, using file.open to read a XML from your file system, and stubbing your method call on your test. This is the most common way of doing some tests with third party apis. But, if you want a more reliable way to do some test with API, I would recommended for you to try VCR. I am not a minitest guy, but I think VCR have setup to works fine with minitest too. Have a nice day man On Sunday, June 9, 2013, Linus Pettersson wrote:> Hi > > I''m building a small library to use in my Rails app which interacts with a > 3rd party API. It fetches artists and tracks. > > So I have: > lib/my_lib.rb > lib/my_lib/artist.rb > lib/my_lib/track.rb > > To get an artist I do > artist = MyLib::Artist.find("Oasis") > Which returns an instance of Artist if it finds (via 3rd party API) it or > nil otherwise. > > Then I can call artist.tracks which fetches all available tracks at the > 3rd party and returns them as an array. > > In my artist class I have a separate method that makes the actual API > calls and parses the XML response with Nokogiri > > def self.find(name: nil) > # Stuff... > doc = get_data(some_url) > # More stuff... > end > > def get_data(req_url) > Nokogiri::XML(open(req_url)) > end > > When writing my tests I don''t want to hit the actual URL but instead load > an XML file from disk. > > I''m using Minitest which has a mocking library, but how do I actually mock > (or is it more appropriate to stub?) the method? > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:_e({}, > ''cvml'', ''rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org'');>. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:_e({}, ''cvml'', ''rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org'');> > . > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/a2e63640-ac9a-4114-a949-7a8caaab7468%40googlegroups.com?hl=en-US > . > For more options, visit https://groups.google.com/groups/opt_out. > > >-- thiagocifani http://about.me/thiagocifani <http://del.icio.us/thiagocifani> <http://del.icio.us/thiagocifani> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAGNtUE8Y_jFkRUgLW1L_KBaxyUhWTT4itM_qeGxxn2ybTvbeCA%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
> On Sunday, June 9, 2013, Linus Pettersson wrote: > > > > Hi > > > > I''m building a small library to use in my Rails app which interacts > > with a 3rd party API. It fetches artists and tracks. > > > > So I have: > > lib/my_lib.rb > > lib/my_lib/artist.rb > > lib/my_lib/track.rb > > > > To get an artist I do > > artist = MyLib::Artist.find("Oasis") > > Which returns an instance of Artist if it finds (via 3rd party API) it > > or nil otherwise. > > > > Then I can call artist.tracks which fetches all available tracks at the > > 3rd party and returns them as an array. > > > > In my artist class I have a separate method that makes the actual API > > calls and parses the XML response with Nokogiri > > > > def self.find(name: nil) > > # Stuff... > > doc = get_data(some_url) > > # More stuff... > > end > > > > def get_data(req_url) > > Nokogiri::XML(open(req_url)) > > end > > > > When writing my tests I don''t want to hit the actual URL but instead > > load an XML file from disk. > > > > I''m using Minitest which has a mocking library, but how do I actually > > mock (or is it more appropriate to stub?) the method? > >thiagocifani <cifani.thiago-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I think you can easily accomplish this task declaring a variable, using > file.open to read a XML from your file system, and stubbing your method > call on your test. This is the most common way of doing some tests with > third party apis. But, if you want a more reliable way to do some test with > API, I would recommended for you to try VCR. I am not a minitest guy, but I > think VCR have setup to works fine with minitest too.I suggest you use both WebMock and VCR. WebMock mocks out the HTTP calls the API code is doing, and lets VCR record the response the first time, and play it back from then on. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/51b51faa.6461b60a.31b0.ffffa401%40mx.google.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.