Ask A Question

Notifications

You’re not receiving notifications from this thread.

Can someone help explain how to use test_double to mock a gem class in my app?

Bill Snapper asked in Testing

Hello,

My app uses a gem that goes out to network devices. I want to write some specs that test my classes without going out to the network devices via the gem. I want to use test_double or something equivalent in my specs so that I can test independent of the gem.

Any pointers to tutorials, info, or examples would be great.

Thanks in advance.

Reply

One thing that comes to mind here is actually the VCR gem. You can use it to record network requests and save them so that your tests can just replay a real request. It sounds like you're describing something that might be a perfect fit for VCR: https://github.com/vcr/vcr

Have you looked into that at all?

Reply

I have not looked at this but will check it out. Honestly just looking to create a dummy instance with responses in getters

Thank you

Reply

One of the downsides of doing that is you're faking out objects so not all your code is fully tested there. VCR would only fake out the HTTP response and you could fully test all of your code instead. Sometimes that can save you from some future troubles where a mock object wouldn't.

On the test double side, I've used rspec-mocks before and it worked pretty well. Most of what I was doing I just used the readme for https://github.com/rspec/rspec-mocks#test-doubles

Basically creating the double and then passing that object into the classes I wanted to test. Then you can expect it to have certain methods called and such which is pretty convenient and mock out the return values.

Those are what can get you into trouble if your object's API changes in the app but you forget to update your test doubles to match exactly. That's why I usually recommend not using them as much as you can so you know you're always operating on the same exact objects the app will have.

Reply

Thanks, I'll check this out when I get time over the weekend and post back on how it worked out.

Reply

Hi Chris, I looked into VCR and tried it out. It's great for being able to utilize gems / classes that send HTTP requests. I ended up not using this in my test suite as I'm hitting thermostats and know the gem that hits them works so I decided to mockup the API writing a simple class that implemented the methods of the API. Not a lot of work and I definitely see the value of using VCR but for this particular application it wasn't the right thing. It will definitely be in my bag of test tricks however for some other projects where I'm hitting some external APIs.

Reply

Great stuff! I would do the same thing. Since I hadn't paid too close attention that you were using a gem, this makes more sense for that situation. My bad! :)

Reply

No worries at all. I happen to be the maintainer for the gem but for this app I don't want or need to perform any operations directly on thermostats. Thanks for the pointer.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.