A gem for null object pattern easy implementation on Ruby objects.
Nilify works with Ruby 1.9.3 onwards. Install this gem with:
gem install 'nilify'
##Example
Let's check an example for the next Item class.
class Item
attr_accessor :sku, :title
end
To create a class with mock methods there are two options:
# Option 1
# on the nil class specify the methods to mock
class NilItem
extend Nilify
nilify [:sku, :title]
end
# Option 2
# Pass the Item class to mock all the defined methods inside that class.
class NilItem
extend Nilify
nilify_from Item
end
Then you can use the mocked methods like this:
> item = Item.new
> item.id
=> nil
> item = NilItem.new
> item.id
=> "id is a mock method"
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
- Everardo Medina (https://github.com/everblut)
- Add better examples
- Add support for ActiveRecord
- Add conditional mocking
- Add expected return value
- Add custom response for mocked methods
- Add wiki
Rubixware - hello@rubixware.com
MIT License. Copyright 2015 Rubixware. http://www.rubixware.com