Module: ElabsMatchers::Matchers::Rspec::Orm

Defined in:
lib/elabs_matchers/matchers/rspec/orm.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) persist

Asserts if an assigned value persistes in the database.

Example: post.should persist(:title, "Blog post")

Parameters:

  • attribute (Symbol)

    name The name of the attibute to check if it persisted or not

  • value (Object)

    The value to persist to the attribute



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/elabs_matchers/matchers/rspec/orm.rb', line 15

RSpec::Matchers.define :persist do |attribute, value|
  match do |actual|
    actual.send(:#{attribute}=", value)
    actual.save!
    actual = actual.class.find(actual.id)
    @final_value = actual.send(attribute)
    @final_value == value
  end
  failure_message_for_should { |actual| "Expected #{attribute} to be persisted and retain its value of #{value.inspect} but the value was #{@final_value.inspect}" }
  failure_message_for_should_not { |actual| "Expected #{attribute} not to be persisted and retain its value of #{value.inspect} but it did." }
end