You sometimes want to test business logic, possibly outside of Rails, where you don’t want to bother with fixtures and or connecting to a database. Structs offer a simple solution when you only want to access and modify data in records and associations and aren’t interested in testing finding or saving the data.
Obviously this is of no use when you hneed to use methods in the classes you’d want to mock with Structs.
Customer = Struct.new(:name, :address, :zip)
Order = Struct.new(:customer,:item, :created_at)
joe = Customer.new("Joe Smith", "1234 Franklin",55455)
jane = Customer.new
jane.name = "jane Smith"
jane.address = "1234 Franklin"
jane.zip = 55455
orders = []
orders << Order.new(joe, "20 2x4","10-10-2008")
orders << Order.new(joe, "hammer","10-11-2008")
orders << Order.new(jane,"nails","10-12-2008)
# Store is an Active Record class
# It won't know 'orders' isn't a collection
#of ActiveRecord::Base::Order class instances
store = Store.new
store.orders = orders
store.orders << Order.new(joe, "paint", "10-13-2008")
assert_equal 1, store.orders_from_unique_addresses
assert_equal 2,store.orders_from_unique_customers
assert_equal 4,store.items_ordered
Having established that the structs function in a trivial way like Active Record objects, you can then use them in the part of your tests requiring this data.