https://github.com/monade/rspec_create_model
rspec_create_model, a rspec matcher to verify that a created model respects what you expect
https://github.com/monade/rspec_create_model
gem rails rspec-matchers ruby
Last synced: about 1 month ago
JSON representation
rspec_create_model, a rspec matcher to verify that a created model respects what you expect
- Host: GitHub
- URL: https://github.com/monade/rspec_create_model
- Owner: monade
- License: mit
- Created: 2022-06-10T13:02:25.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-07-29T15:08:30.000Z (almost 3 years ago)
- Last Synced: 2025-04-12T10:12:55.626Z (about 1 month ago)
- Topics: gem, rails, rspec-matchers, ruby
- Language: Ruby
- Homepage: https://monade.io/en/home-en/
- Size: 14.6 KB
- Stars: 2
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://badge.fury.io/rb/rspec_create_model)# 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
```ruby
gem 'rspec_create_model'
```## Example usage
Given the following records:
```ruby
class Author < ActiveRecord::Base
attribute :name, type: :String
has_many :articles, dependent: :destroyafter_create :create_empty_book
def create_empty_book
Book.create!(author_id: id, title: "First Empty Book")
end
endclass Book < ActiveRecord::Base
belongs_to :author, optional: false
attribute :title, type: :String
end
```You can match created records like this:
```ruby
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:
```ruby
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](https://opensource.org/licenses/MIT).
About Monade
----------------
rspec_create_model is maintained by [mònade srl](https://monade.io/en/home-en/).
We <3 open source software. [Contact us](https://monade.io/en/contact-us/) for your next project!