README.md in active_mocker-1.4.2 vs README.md in active_mocker-1.5
- old
+ new
@@ -1,21 +1,26 @@
# ActiveMocker
+[![Gem Version](https://badge.fury.io/rb/active_mocker.svg)](http://badge.fury.io/rb/active_mocker)
[![Build Status](https://travis-ci.org/zeisler/active_mocker.png?branch=master)](https://travis-ci.org/zeisler/active_mocker)
[![Code Climate](https://codeclimate.com/github/zeisler/active_mocker.png)](https://codeclimate.com/github/zeisler/active_mocker)
+[![Dependency Status](https://gemnasium.com/zeisler/active_mocker.svg)](https://gemnasium.com/zeisler/active_mocker)
+[![Gitter chat](https://badges.gitter.im/zeisler/active_mocker.png)](https://gitter.im/zeisler/active_mocker)
ActiveMocker creates mocks classes from ActiveRecord models. Allowing your test suite to run very fast by not loading Rails or hooking to a database. It parses the schema definition and the defined methods on a model then saves a ruby file that can be included within a test. Mocks are regenerated when the schema is modified so your mocks will not go stale. This prevents the case where your units tests pass but production code is failing.
Example from a real app
Finished in 0.54599 seconds
190 examples, 0 failures
------------------------------------------
-
+* [Contact](#contact)
* [Installation](#installation)
* [Setup](#setup)
+ * [Configuration](#overwrite_defaults_configuration)
+ * [Generate](#generate_mocks)
* [Dependencies](#dependencies)
* [Usage](#usage)
* [Mocking Methods](#mocking-methods)
* [Managing Mocks](#managing-mocks)
* [ActiveRecord supported methods](#activerecord-supported-methods)
@@ -24,11 +29,17 @@
* [Contributing](#contributing)
------------------------------------------
+## Contact
+Ask a question in the [chat room](https://gitter.im/zeisler/active_mocker).
+
+------------------------
+
+
## Installation
Add this line to your application's Gemfile:
gem 'active_mocker'
@@ -46,39 +57,25 @@
* Requires Ruby MRI =< 2.1.
## Setup
-### Configure the Mock Generator
- config/initializers/active_mocker.rb
+### Overwrite defaults configuration
ActiveMocker::Generate.configure do |config|
- # Required Options
config.schema_file = File.join(Rails.root, 'db/schema.rb')
config.model_dir = File.join(Rails.root, 'app/models')
config.mock_dir = File.join(Rails.root, 'spec/mocks')
- # Logging
config.logger = Rails.logger
end
-### Create a Rake Task to Auto Regenerate Mocks
+### Generate Mocks
-Here is an example of a rake task to regenerate mocks after every schema modifiation. If the model changes this rake task needs to be called manually. You could add a file watcher for when your models change and have it run the rake task.
+Running this rake task builds/rebuilds the mocks. It will be ran automatically after every schema modification. If the model changes this rake task needs to be called manually. You could add a file watcher for when your models change and have it run the rake task.
- lib/tasks/active_mocker.rake
+ rake active_mocker::build
- task rebuild_mocks: :environment do
- puts 'rebuilding mocks'
- ActiveMocker.create_mocks
- end
-
- ['db:schema:load', 'db:migrate', 'db:reset'].each do |task|
- Rake::Task[task].enhance do
- Rake::Task['rebuild_mocks'].invoke
- end
- end
-
## Usage
#db/schema.rb
ActiveRecord::Schema.define(version: 20140327205359) do
@@ -156,28 +153,26 @@
### Class Methods
PersonMock.bar('baz')
=> RuntimeError: ::bar is not Implemented for Class: PersonMock
- PersonMock.mock_class_method(:bar) do |name, type=nil|
+ # Rspec 3 Mocks
+ allow(PersonMock).to receive(:bar) do |name, type=nil|
"Now implemented with #{name} and #{type}"
end
### Instance Methods
PersonMock.new.bar('foo', 'type')
=> "Now implemented with foo and type"
- PersonMock.mock_instance_method(:bar) do
+ # Rspec 3 Mocks
+ allow_any_instance(PersonMock).to receive(:bar) do
"Now implemented"
end
- # override mock on an individual instance
- PersonMock.new.mock_instance_method(:bar) do
- "Now implemented!!!!"
- end
#### When the model changes, the mock fails
(Requires a regeneration of the mocks files.)
#app/models/person.rb
@@ -213,17 +208,26 @@
--------------
#person_spec.rb
- person_mock.mock_instance_method(:bar) do |name, type=nil|
+ # Rspec 3 Mocks
+ allow(person_mock).to receive(:bar) do |name, type=nil|
"Now implemented with #{name} and #{type}"
end
=> NoMethodError: undefined method `bar' for class `PersonMock'
### Managing Mocks
+Rspec Tag - active_mocker:true
+
+ describe 'Example', active_mocker:true do
+
+ end
+
+ Assigning this tag will stub any ActiveRecord model Constants for Mock classes in any `it's` or `before(:each)`. To stub any Constants in `before(:all)`, `after(:all)` use `mock_class('ClassName')`.
+
Deletes All Records and Clears Mocked Methods
PersonMock.clear_mock
Clears all Loaded Mocks - (Use in after(:all) to keep state from leaking to other tests.)
@@ -241,13 +245,15 @@
Map The Mock Class to it's Model
ActiveMocker::LoadedMocks.class_name_to_mock
=> { 'Person' => PersonMock }
+
-### Constants are Available - (Modules and classes are excluded.)
+### Constants and included and extended Modules are Available.
+
#app/models/person.rb
class User < ActiveRecord::Base
CONSTANT_VALUE = 13
end
@@ -336,12 +342,12 @@
* Default value
* Scale and Precision not supported.
### Known Limitations
* Model names and table names must follow the default ActiveRecord naming pattern.
-* Included/extended module methods will not be included on the mock. I suggest you keep domain logic out of the model and only add database queries. Domain logic can be put into modules and then included into the mock during test setup.
-* Whatever associations are setup in one mock object will not effect any other objects.
+* Whatever associations are setup in one mock object will not reflected in any other objects.
* Validation are not present in mocks.
+* Sql queries, joins, etc will never be supported.
## Inspiration
Thanks to Jeff Olfert for being my original inspiration for this project.
## Contributing