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 =, :address, :zip)
Order =,:item, :created_at)

joe ="Joe Smith", "1234 Franklin",55455)
jane = = "jane Smith"
jane.address = "1234 Franklin" = 55455

orders = []
orders <<, "20 2x4","10-10-2008")
orders <<, "hammer","10-11-2008")
orders <<,"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.orders = orders
store.orders <<, "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.