# Testing classes making API calls

**URL:** <https://www.ruby-forum.com/t/testing-classes-making-api-calls/165822>\
**Category:** Ruby\
**Created:** [April 17, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/testing-classes-making-api-calls/165822 "2009-04-17T21:40:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [April 17, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/testing-classes-making-api-calls/165822/1 "2009-04-17T21:40:40Z")

</div>

I am currently working on a project which gets a lot of info from the  
YouTube API and does some data mining with it.  
However I would really like to have the best test coverage possible. I  
thought about writing a mock up class that replaces the class making all  
the YouTube API calls for tests. But I still would be able to get the  
class, making the calls, covered since all my results so heavily depend  
on this.

Are there any best practices for these situation?

Thank you very much for all answers!

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [April 17, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/testing-classes-making-api-calls/165822/2 "2009-04-17T21:50:54Z")

</div>

Hi Alexander,

I’ve run into the same problem and I did exactly what you’re suggesting.  
You don’t want to slow down your tests by making external API calls and  
since I’m assuming that you already know the behavior of the YouTube  
API, it makes sense to mimic that behavior in a mock class. Also, you  
will get great coverage results by doing this 🙂

Joseph Gutierrez  
Web D. - Inc21  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [April 17, 2009, 10:11pm UTC](https://www.ruby-forum.com/t/testing-classes-making-api-calls/165822/3 "2009-04-17T22:11:30Z")

</div>

> I’ve run into the same problem and I did exactly what you’re suggesting.  
> You don’t want to slow down your tests by making external API calls and  
> since I’m assuming that you already know the behavior of the YouTube  
> API, it makes sense to mimic that behavior in a mock class. Also, you  
> will get great coverage results by doing this 🙂

When mocking an API, I often write a test like this:

def toast\_youtuber  
yt = YouTuber.new  
puts yt.fetch(‘some\_feed’)  
end

It’s a “toast” - a temporary test that will toast your test run if you  
accidentally integrate it. Switch it from “toast” to “test” in your  
editor,  
run it, and inspect the output.

Switch it back to “toast”, and copy the output into a sample file. Stash  
that in your test/fixtures folder, and use it when you mock the wire  
connection.

def test\_youtuber\_fetch  
yt = YouTuber.new  
yt.expects(:fetch).returns(File.read(RAILS\_ROOT +  
‘/test/fixtures/youtuber.xml’))  
yt.fetch(‘some\_feed’)  
assert{ yt.feed == ‘some\_data’ }  
end

The .expects is a Mocha mock. Note we don’t mock the whole object - just  
the  
low-level part that hits the wire. (Unit tests that hit any outgoing  
wires  
are not just slow, they are evil.)

Whenever you need to upgrade your mock data, just turn on the toast test  
again and pull another real feed.
