#60 Testing without Fixtures

Tests which rely heavily on fixtures are brittle and can be difficult to maintain. This episode will show a couple techniques for creating tests which don't use fixtures.
# cart_test.rb
def test_total_weight_should_be_sum_of_line_item_weights
  cart = Cart.new
  cart.line_items.build.stubs(:weight).returns(7)
  cart.line_items.build.stubs(:weight).returns(3)
  assert_equal 10, cart.total_weight
end

# line_item_test.rb
def test_should_have_zero_for_weight_when_not_shipping
  line_item = LineItem.new
  line_item.build_delivery_method(:shipping => false)
  assert_equal 0, line_item.weight
end

def test_should_have_weight_of_product_times_quantity_when_shipping
  line_item = LineItem.new(:quantity => 3)
  line_item.build_delivery_method(:shipping => true)
  line_item.build_product(:weight => 5)
  assert_equal 15, line_item.weight
end

你可能感兴趣的:(test)