Double an instance of a Ruby class -
I'm using Ruby on Rail Some hardware available on my development I do not want
using Rails.env want a class "double" so that "real implementation"
Double square XY iff Rails.env ==: Production
).
I have tried rspec-mocks double
, but it requires throws expectations and exceptions.
Finally I used the following code:
class double The DRF method_mission puts (M, * algies, and blocks) "# {m} was called with logic: # {args.join (',')}" end and
Of course, it does not work for those methods, which have been declared on objects
, but it was sufficient for my needs, apart from this, it does not give the ability to pass in the parameter.
In addition to this, I wrote a little helpful function to instantiate objects based on class constants. Since there were a few singletons in my code, I also implemented a check for this, so that the actual class is reversed only when the railway environment is 'produced'.
def example_f (c) if Rails.env == 'output' if c.ancestors.include? Singleton c.instance else c.new end second double. New End End
Example Usage: SOME_CONST = Module XY :: ClassZ
.
Comments
Post a Comment