Modelizer¶ ↑
Description¶ ↑
Need a simple, consistent way to create model instances and check validations in your ActiveRecord 3.1+ tests? Use the Modelizer. Just don’t trust the docs, since lots changed in 5.x and I got lazy.
Examples¶ ↑
First, enable the Modelizer in your test helper. In Rails, try something like:
require "modelizer/all" class ActiveSupport::TestCase include Modelizer extend Modelizer::Validations end
Next, define some fixtures and factories. Modelizer will load these when the module is included, using "test/{factories,fixtures}/*/.rb"
as a default glob. Change it by setting Modelizer.glob
before module inclusion.
Fixture and factory definitions look remarkably similar. The only real difference is that fixtures get loaded into the DB once at module inclusion and factories don’t. Factories will also generally use autogenerated/random data generators. Some examples:
# from test/fixtures/user.rb fixture :user, User do |u| u.account = use :account u.email = "default@example.org" u.name = "A User" u.password = "123456" u.state = "active" end # from test/factories/user.rb factory :user, User do |u| u.account = use :account u.email = Faker::Internet.email u.name = Faker::Name.name u.password = Faker::Lorem.words(1).first end
To get a reference to a fixture, the use
method is added to your test class:
def test_something refute_equal use(:artist).name, use("artist/child").name end
To get an instance from a factory, the build
and create
methods are available:
def test_something_else assert_equal "foo", build(:artist, name: "foo").name refute create(:artist).new_record? end
Custom Assertions¶ ↑
If you require "modelizer/all"
or include Modelizer::Assertions
, Modelizer adds one additional assertion, assert_invalid
.
assert_invalid :email, model, /is bad/
The third argument is optional.
def test_pointless_stuff assert new_user.valid? assert_invalid :name, build(:artist, name: nil) end
Testing Validations¶ ↑
I should really write some docs for this.
Installation¶ ↑
$ gem install modelizer
License¶ ↑
Copyright 2009-11 John Barnette (code@jbarnette.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.