Module: RSpec::ActiveRecord

Includes:
StubModels
Defined in:
lib/rspec/active_record.rb,
lib/rspec/active_record/matcher.rb,
lib/rspec/active_record/version.rb,
lib/rspec/active_record/stub_models.rb,
lib/rspec/active_record/change_record.rb,
lib/rspec/active_record/create_record.rb,
lib/rspec/active_record/destroy_record.rb,
lib/rspec/active_record/record_matcher.rb,
lib/rspec/active_record/diff_for_multiple_records.rb

Overview

Various helpers and matchers to help with specs when working with ActiveRecord models. It is automatically included in RSpec specs.

Defined Under Namespace

Modules: StubModels Classes: ChangeRecord, CreateRecord, DestroyRecord, DiffForMultipleRecords, Matcher, RecordMatcher

Constant Summary collapse

VERSION =
"0.3.2"

Instance Method Summary collapse

Methods included from StubModels

#create_temporary_table, #stub_class, #stub_model

Instance Method Details

#change_record(record) ⇒ ChangeRecord

Allows matching that code inside a block changed specific record.

Examples:

Block updated user name to specific one

expect { user.update!(name: "RSpec User") }.to change_record(user).to(name: "RSpec User")

Block updated user name from specific one

expect { user.update!(name: "RSpec User") }.to change_record(user).from(name: "Initial Name")

Parameters:

  • record (ActiveRecord::Base)

    Model that we're checking for changes

Returns:



42
43
44
# File 'lib/rspec/active_record.rb', line 42

def change_record(record)
  ChangeRecord.new(record)
end

#create_record(scope) ⇒ CreateRecord

Allows matching that code inside a block created specific record.

Examples:

Block created any User

expect { User.create!(name: "RSpec User") }.to create_record(User)

Block create a User matching specific name

expect { User.create!(name: "RSpec User") }.to create_record(User).matching(name: "RSpec User")

Block create a User matching specific name

expect { User.create!(name: "RSpec User") }.to create_record(User.where(name: "RSpec User"))

Parameters:

  • scope (ActiveRecord::Relation, Class)

    Model class or a record where the record should be created.

Returns:



31
32
33
# File 'lib/rspec/active_record.rb', line 31

def create_record(scope)
  CreateRecord.new(scope)
end

#destroy_record(record) ⇒ ChangeRecord

Allows matching that code inside a block destroyed specific record.

Examples:

Block destroyed record

expect { user.destroy! }.to destroy_record(user)

Parameters:

  • record (ActiveRecord::Base)

    Model that should be destroyed

Returns:



51
52
53
# File 'lib/rspec/active_record.rb', line 51

def destroy_record(record)
  DestroyRecord.new(record)
end