# Minitest mock or stub methods

**URL:** <https://www.ruby-forum.com/t/minitest-mock-or-stub-methods/235248>\
**Category:** Rails\
**Created:** [June 9, 2013, 4:10pm UTC](https://www.ruby-forum.com/t/minitest-mock-or-stub-methods/235248 "2013-06-09T16:10:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [June 9, 2013, 4:10pm UTC](https://www.ruby-forum.com/t/minitest-mock-or-stub-methods/235248/1 "2013-06-09T16:10:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [June 9, 2013, 5:14pm UTC](https://www.ruby-forum.com/t/minitest-mock-or-stub-methods/235248/2 "2013-06-09T17:14:14Z")

</div>

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 P. wrote:

> To get an artist I do  
> def self.find(name: nil)  
> an XML file from disk.  
> To post to this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\<javascript:\_e({}, ‘cvml’,  
> 'removed\_email\_address@domain.invalid’);\>  
> .  
> To view this discussion on the web visit

> **[Minitest mock or stub methods](https://groups.google.com/g/rubyonrails-talk/c/Kp_XX4QHpbc/m/owbPrj5WMogJ?hl=en-US)**

> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

–  
thiagocifani  
[Thiago Cifani - Brazil, Software Developer | about.me](http://about.me/thiagocifani) [http://del.icio.us/thiagocifani](http://del.icio.us/thiagocifani)  
[http://del.icio.us/thiagocifani](http://del.icio.us/thiagocifani)

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [June 10, 2013, 2:38am UTC](https://www.ruby-forum.com/t/minitest-mock-or-stub-methods/235248/3 "2013-06-10T02:38:25Z")

</div>

> > ```
> > lib/my_lib/track.rb
> > calls and parses the XML response with Nokogiri
> > 
> > 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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
