Mocked Fixtures plugin
Plugin details
Documentation
ruby script/plugin install git://github.com/adzap/mocked_fixtures.git
== FEATURES/PROBLEMS:
* Reuse your fixtures as mock model objects without database overhead
* Adds feature to supported mocking libraries to quickly create empty mocked model with all attributes methods stubbed out. Supported are Rspec, flexmock and mocha.
* Same familiar style of using regular fixtures
* Works with popular testing frameworks such as Rspec (with rspec-rails), shoulda and any testing library which uses good old test/unit as its base.
What it doesn't do:
* Touch the database. This means that the fixtures are not inserted in the tables, thats not what the plugin is for.
* Create mock objects with the attribute setters. Only reader methods.
SYNOPSIS:
=============
To get going you installed as a gem you need to require the plugin at the top of your test_helper or spec_helper file. If you installed as a Rails plugin then you can skip that step.
If you are using test/unit then you need specify which mocking library you are using in your test_helper like so:
class Test::Unit::TestCase # can be one of :flexmock or :mocha self.mock_fixtures_with :flexmock end
If you are using Rspec you need to set it in the configure block:
Spec::Runner.configure do |config| # can be one of :rspec, :flexmock or :mocha config.mock_fixtures_with :rspec end
If you are using something other than the supported libraries then you get an error alerting the plugin can't be used with that library. You can then write you own interface to the plugin for that library if you want. See the files in lib/mocked_fixtures/mocks and the spec for mock fixture objects in spec/mock_factory_spec.rb.
On to the good stuff. Now if you have a Company model and fixture for the model like this
megacorp: name: Mega Corporation moto: Do Evil
Just like normal fixtures you declare which ones you want to use in your test or spec at the top
class MyTest < Test::Unit:TestCase mock_fixtures :companies def setup @company = mock_companies(:megacorp) end def test_does_something assert_equal @company.moto, 'Do Evil' end end
All the attributes will be stubbed to return the values from the fixture. The fixture is generated using the internal Rails fixtures class, so any of fixture tricks, such as association labels and automatically generated ids, will work.
If you want more than one fixture, then like normal fixtures you list the fixture keys to get back an array of the objects.
@companies = mock_companies(:megacorp, :bigstuff)
To quickly grab all of the fixtures you call use the :all option
@companies = mock_companies(:all)
You can also pass a block to the fixture accessor method to neatly customize the mock object for local use
@company = mock_companies(:megacorp) do |c| c.stub!(:some_method).and_return('special value') end
The block is called on all mock fixtures returned.
Like regular fixtures you can also declare global mock fixtures to pre-load for all your tests. Because the mock fixtures don't access the database it doesn't slow down the running of tests like global fixtures can.
To setup global mock fixtures, in your test_helper put:
class Test::Unit::TestCase self.global_mock_fixtures = :companies end
If you are using Rspec you need to set it in the configure block:
Spec::Runner.configure do |config| config.global_mock_fixtures = :companies end
You can also set the global to :all and all fixtures will be loaded as mocks. This can add some test startup time if you have a lot of fixtures but shouldn't slow down your tests when running at all. Might use a bit of memory though.
Thats all for now, so mock on!
NOTE:
If you use MS SQL Server then you need to apply a fix for this adapter to correctly dump primary keys when not named 'id'. Just drop this line in an initializer file
require 'mocked_fixtures/connection_adapters/sqlserver_adapter'
You then need to do a dump. Um, keep your pants on, I mean a database dump with
rake db:schema:dump
Now the schema.rb file should be complete.
Further Documentation
There is currently no advanced documentation for this plugin.
New documentationEdit plugin | (0 older versions) | Last edited by: hardway, 2 months ago

