picardo
picardo

Reputation: 24886

How to test non-Rack web services in Rails with Rspec?

I am building a Rails app that will need to utilize a web service in Node, a single endpoint that sends a streaming response. I've been thinking about how to test this, and thought to myself why can't I use RSpec for this rather than using a Javascript testing library? It's just one endpoint, and there is no telling if I will use Node extensively in the future.

So my question is this: how can I test this API endpoint using RSpec? I found this instructive tutorial for Rack-based apps, but I wonder if there are similar things for non-Rack based apps, as well.

Upvotes: 0

Views: 77

Answers (1)

Justin M
Justin M

Reputation: 1065

I would keep tests close to the code and as portable an environment as possible. If you really want to test it with RSpec, create a class that acts as a service wrapper around an API endpoint - e.g., MyMusicService, that has methods for fetching, posting, or whatever to the URL via something like Typhoeus. Then summon that service in RSpec. If you don't need that level of abstraction, then you can use Typhoeus in Rspec directly.

Upvotes: 2

Related Questions