Project

mimic-rb

0.0
No commit activity in last 3 years
No release in over 3 years
Minimalistic Library for Mocking, Stubbing, and Faking Classes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0
>= 0
 Project Readme

#Mimic Build Status Code Climate

##Rspec style mocking and stubbing for NullObjects, Fakes, and more!##

Inspired by r00k's talk at magicruby, I decided to work on a minimalistic library for mocking and stubbing full classes. The original purpose would be to have a NullObject consistently stay in sync with its real object equivelant with minimal effort.

Installing

gem install mimic-rb

or

#Gemfile
gem 'mimic-rb'
bundle install

Usages

Given we have an awesome class

class NameResponder

  def hello
    "hi"
  end

  def name
    "John"
  end

  def full_name(first, last)
    "#{first} #{last}"
  end

  def self.reverse(name)
    name.reverse
  end

end

Then in our NullObject / Fake / Mimic we simply need to include it and use it.

class FakeResponder
  include Mimic
  mocks NameResponder

  stubs(:name) { "None" }

  class_stubs(:reverse) { "no name" }
end

Then anywhere we would use NameResponder we can use FakeResponder and it will respond to all calls. The default return value for all methods is simply nil, but with the stubs(:method) { result } method you can specify what you want to be returned.

responder = NameResponder.new
fake      = FakeResponder.new

responder.hello #=> "hi"
fake.hello      #=> nil

responder.name  #=> "John"
fake.name       #=> "None"

responder.full_name "Jane", "Doe" #=> "Jane Doe"
fake.full_name "Avdi", "Burnhart" #=> nil

NameResponder.reverse "Ryan" #=> "nayR"
FakeResponder.reverse "Ryan" #=> "no name"

Error Handling

To keep you from stubbing methods that don't exist and thus getting your fake out of sync with your base class, Mimic throws two custom exceptions based on what travesty you committed.

If you try to stub an instance method that does not belong on the base class, you will get InstanceMethodNotDefined Error.

If you try to stub a class method that does not belong on the base class, you will get ClassMethodNotDefined Error.

Pretty self explanatory. These are simply here as a gentle reminder that will help keep your fake in sync with your base class before you get in production and start chasing nil and NoMethod errors.

Developing for Mimic

If you have any feature requests or bug fixes, I actively encourage you to try out integrating those changes yourself and making a pull request.

First fork the repo

git clone [GITHUB GIT URL]
bundle
rspec

Check all the tests and then from there have fun!

TODO

* Test that setters and getters are also properly mocked/stubbed
* See if it'd be a horrible idea to mix this into ruby's class so you don't have to `include Mimic` in your mimic classes
* Module Stubbing/Mocking
* Strict Parameter checking. **Currently all mocked methods allow n arguments.**