= SixArm.com » Ruby » ActiveRecord mock object for testing Rails
Author:: Joel Parker Henderson, joel@joelparkerhenderson.com
Copyright:: Copyright (c) 2006-2011 Joel Parker Henderson
License:: See LICENSE.txt file
A simple mock object that provides the ActiveRecord method
signatures read_attribute(key) and write_attribute(key,val),
and simple record finder signatures find(id) and find(:all).
This provides some of the ActiveRecord method signatures we use:
- read_attribute(key)
- write_attribute(key,val)
- find(id)
- find(:all)
Example:
mock = ActiveRecordMock.new
mock.write_attribute('foo','bar')
mock.read_attribute('foo') => 'bar'
Example of initialize with attributes:
mock = ActiveRecordMock.new(:foo => 'bar', :goo => 'car', :hoo => 'dar')
mock.read_attribute(:foo') => 'bar'
mock.read_attribute(:goo') => 'car'
mock.read_attribute(:hoo') => 'dar'
Example of creating mock users:
anne = ActiveRecordMock.new(:id => 123, :name => 'Anne')
beth = ActiveRecordMock.new(:id => 456, :name => 'Beth')
cate = ActiveRecordMock.new(:id => 789, :name => 'Cate')
Example of mock finder creation:
ActiveRecordMock.find=[anne,beth,cate]
Example of mock finder retrieval of records by id:
ActiveRecordMock.find(123) => anne
ActiveRecordMock.find(456) => beth
ActiveRecordMock.find(789) => cate
Example of mock finder retrieval of all records:
ActiveRecordMock.find(:all) => [anne,beth,cate]