New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

model_mocker

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

model_mocker

1.1.3
Rubygems
Version published
Maintainers
1
Created
Source

= ModelMocker

This plugin gives you an easy way of generating partially mocked ActiveRecord model objects. This is a useful way of simulating some aspects of a model (like the persistence layer) but leaving the domain logic parts intact, so that you test those in isolation, without worry about database reads and writes.

It's similar to RSpec's mock_model method, but gives you a slightly more declarative way of going about things. I found myself wanting something along these lines a long time ago, and created something very simple that I've used in several projects. More recently I've found myself wanting to easily control certain kinds of behaviour in controller specs, like wanting to ensure that a model instance is valid to allow easy and declarative speccing of the controller.

The plugin adds a mock method to ActiveRecord::Base when mock_models is required, which behaves a lot like AR::Base.new or AR::Base.create, except that you can specify an ID too. Special behaviour is added by passing a block to mock and calling methods on the object it yields.

== Installation

For Rails 2, add model_mocker to your config.gem for the test evironment.

For Rails 3, add model_mocker to your +Gemfile+ in the :test group.

== Requirements

ModelMocker uses the Mocha[http://mocha.rubyforge.org/] Gem.

== Examples

To create a mocked model instance which behaves as it's been fetched from the DB:

ModelClass.mock(:id => 1)

To create one which behaves as if it's a new record

ModelClass.mock { |m| m.as_new_record }

One which is new and will always report that is invalid, whether or not ActiveRecord validations have been met or not.

ModelClass.mock do |m|
  m.as_new_record
  m.invalid
end

The RDoc for the instance methods on ModelMocker gives a complete rundown of what's possible.

Copyright (c) 2008 Matt Patterson, released under the MIT license

FAQs

Package last updated on 04 Aug 2010

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts