Implements helper methods & matchers when working with RSpec & ActiveRecord.
Add it to Gemfile:
group :test do
gem "rspec-active_record", require: false
end
And require it in your rails_helper
or spec_helper
after rspec/rails
:
require "rspec/active_record"
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install rspec-active_record
Check that block creates a record:
expect { User.create!(name: "RSpec User") }.to create_record(User)
You can also make sure that attributes match, if it fails you'll get RSpec diff between created record and what you expected:
expect { User.create!(name: "RSpec User") }.to create_record(User).matching(name: "RSpec User")
You can also achieve similar results using a scope, but not that in this case you won't see a diff:
expect { User.create!(name: "RSpec User") }.to create_record(User.where(name: "RSpec User"))
Check that code updates attributes of your record (note that it will automatically refind the record, so make sure changes are saved):
expect { user.update!(name: "RSpec User") }.to change_record(user).to(name: "RSpec User")
Sometimes it's useful to specify what the attributes should've been initially:
expect { user.update!(name: "RSpec User") }.to change_record(user).from(name: "Initial Name")
Check that code destroys a record:
expect { user.destroy! }.to destroy_record(user)
Stub class for a spec, pass a block to customize the class:
stub_class :DummyDecorator, ApplicationDecorator do
def object
Object.new
end
end
DummyDecorator.new.object #=> #<Object>
Similar to stub_class
but automatically inherits from ApplicationRecord:
stub_model :DummyUser do
belongs_to :client, optional: true
end
DummyUser.new.client #=> nil
Requires database that supports modifying structure inside transaction. Combines well with stub_model
to create table for stubbed model:
create_temporary_table :dummy_users do |t|
t.belongs_to :client
end
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/andriusch/rspec-active_record.
The gem is available as open source under the terms of the MIT License.