No release in over a year
An RSpec matcher to match the structure of a given created model.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

 Project Readme

Tests Gem Version

rspec_create_model

A rspec matcher for Rail's ActiveRecord, to check the type of models created from actions.

Installation

Add the gem to your Gemfile

  gem 'rspec_create_model'

Example usage

Given the following records:

class Author < ActiveRecord::Base
  attribute :name, type: :String
  has_many :articles, dependent: :destroy

  after_create :create_empty_book

  def create_empty_book
    Book.create!(author_id: id, title: "First Empty Book")
  end
end

class Book < ActiveRecord::Base
  belongs_to :author, optional: false
  attribute :title, type: :String
end

You can match created records like this:

expect { Author.create!(name: "Some author name") }.to create_model(Author)

expect { Author.create!(name: "Some author name") }
  .to create_model(Author)
  .and create_model(Book)

When the matcher is called, 2 instace variable get set: @created_record and @created_records

these variables includes the newly created records. You can also use them if you want, like this:

expect { Author.create!(name: "Some author name") }.to create_model(Author)

expect(@created_record).to be_a(Author)
expect(@created_records).to all(be_a(Author))

including the created records, you also could write tests on their content.

Remember to checkout the Gem's specs to see how to use it!

License

The gem is available as open source under the terms of the MIT License.

About Monade

monade

rspec_create_model is maintained by mònade srl.

We <3 open source software. Contact us for your next project!