0

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.

sites
  • 21,417
  • 17
  • 87
  • 146

2 Answers2

1

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

CamelBlues
  • 3,444
  • 5
  • 29
  • 37
0

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.

Chris Mowforth
  • 6,689
  • 2
  • 26
  • 36