Module: RSpec::ActiveModel::Mocks::Mocks::ActiveRecordInstanceMethods

Defined in:
lib/rspec/active_model/mocks/mocks.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object Also known as: _read_attribute

Transforms the key to a method and calls it.



56
57
58
# File 'lib/rspec/active_model/mocks/mocks.rb', line 56

def [](key)
  send(key)
end

#association(association_name) ⇒ Object

Returns an object representing an association from the mocked model's perspective. For use by Rails internally only.



71
72
73
74
# File 'lib/rspec/active_model/mocks/mocks.rb', line 71

def association(association_name)
  @associations ||= Hash.new { |h, k| h[k] = Association.new(k) }
  @associations[association_name]
end

#destroyObject

Stubs persisted? to return false and id to return nil.



50
51
52
53
# File 'lib/rspec/active_model/mocks/mocks.rb', line 50

def destroy
  RSpec::Mocks.allow_message(self, :persisted?).and_return(false)
  RSpec::Mocks.allow_message(self, :id).and_return(nil)
end

#new_record?Boolean

Returns the opposite of persisted?

Returns:

  • (Boolean)


65
66
67
# File 'lib/rspec/active_model/mocks/mocks.rb', line 65

def new_record?
  !persisted?
end