Skip to content

Latest commit

 

History

History
74 lines (48 loc) · 1.92 KB

README.md

File metadata and controls

74 lines (48 loc) · 1.92 KB

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!