aniel Higginbotham wrote:
involve 5-10 tables and some of the data is different in nearly every
Jack
I can’t release the actual code, and it wouldn’t really make sense
outside of my employer’s unique situation, but here is a rough example
that should illustrate the idea.
The scenario is a delivery company. In particular, testing code that
estimates the profit of jobs. In this simplified scenario we will have
only six tables: drivers, cargo types, trucks types, customers, jobs,
and job cargoes. Drivers contain the pay per mile. Truck types both
contain the maintenance cost per mile of operation as well as the fuel
efficiency. Cargoes contains the pay per mile for different cargoes.
Customers can contain a discount percentage if they are under a bulk
contract. A job has one driver, has one truck type, has one customer,
and has many job cargoes. A job also contains the distance traveled and
the current price of fuel.
Tests would something look like this:
def test_job_with_customer_discount
driver_pay_per_mile 0.43
truck_costs :maintenance_per_mile => 0.25, :miles_per_gallon => 7.6
customer_discount 0.8
cargo :amount => 200, :pay => 7.25
cargo :amount => 5, :pay => 30.0
fuel_price 3.20
distance 850
assert_estimated_profit 400
end
With fixtures you would have the following:
def test_job_with_customer_discount
assert_equal 400, jobs(:with_customer_discount).estimate_profit
end
But you would need to hop through 6 YML files keeping track of all FK’s
in your head to figure out what it was actually testing against.
To make the DSL style test work you would have something like this:
def setup
@job = Job.new
end
def driver_pay_per_mile( p )
@job.driver = Driver.new( :pay_per_mile => p, … other required stuff
for valid driver )
end
def truck_costs( hash )
@job.truck_type = TruckType.new( hash.merge( …other required truck
type stuff ) )
end
def customer_discount( discount )
@job.customer = Customer.new( :discount => discount, … other
customer stuff )
end
def cargo( hash )
new_cargo_type = CargoType.new :pay_per_mile => hash[:pay_per_mile,
… other cargo type stuff
@job.job_cargoes<< JobCargo.new( :cargo_type => new_cargo_type,
:amount => hash[:amount]
end
def fuel_price( price )
@job.fuel_price = price
end
def distance( miles )
@job.distance = miles
end
def assert_profit( p )
assert_equal p, @job.estimate_profit
end
Jack