Module: RSpec::Rails::Mocks::ActiveRecordInstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Transforms the key to a method and calls it.



55
56
57
# File 'lib/rspec/rails/mocks.rb', line 55

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.



66
67
68
69
# File 'lib/rspec/rails/mocks.rb', line 66

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.



49
50
51
52
# File 'lib/rspec/rails/mocks.rb', line 49

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)


60
61
62
# File 'lib/rspec/rails/mocks.rb', line 60

def new_record?
  !persisted?
end