Question

How could I mock any method without using any gem?

Suppose I have access to an complex object like request

How could I make

request.location.country

to return a mocked value.

I read I can mock geocoder results with gems, but I would like to know a generic way.

Was it helpful?

Solution

Here's how I've been doing it

foo = Geocoder.search('Paris, TX')
foo.first.county

I'm using the BingAPI, and to figure out what options I can access on the foo.first object, I take a look at the Geocoder docs:

http://rdoc.info/github/alexreisner/geocoder/master/Geocoder/Result/Bing

OTHER TIPS

As far as Ruby is a duck-typed language where the exact object type isn't verified AOT, then dependency injection is as simple as writing a class that has the same methods/signatures/return types as the real one.

For doing proper mocking, I've used Flexmock in the past with some success, but YMMV depending on what you're doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top