Sha256: 455a67ef33cc235a3a66662597be46cc04702263cbd45b6df0c84846a883f78d

Contents?: true

Size: 1.7 KB

Versions: 89

Compression:

Stored size: 1.7 KB

Contents

class Order

  attr_accessor :shipped_on

  def total_cost
    line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
  end

  def total_weight
    line_items.inject(0) { |total, line_item| total + line_item.weight }
  end

  def shipping_cost
    total_weight * 5 + 10
  end

  class << self

    def find_all
      # Database.connection.execute('select * from orders...
    end
  
    def number_shipped_since(date)
      find_all.select { |order| order.shipped_on > date }.length
    end

    def unshipped_value
      find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
    end

  end

end

require 'test/unit'
require 'mocha'

class OrderTest < Test::Unit::TestCase

  # illustrates stubbing instance method
  def test_should_calculate_shipping_cost_based_on_total_weight
    order = Order.new
    order.stubs(:total_weight).returns(10)
    assert_equal 60, order.shipping_cost
  end

  # illustrates stubbing class method
  def test_should_count_number_of_orders_shipped_after_specified_date
    now = Time.now; week_in_secs = 7 * 24 * 60 * 60
    order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
    order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
    Order.stubs(:find_all).returns([order_1, order_2])
    assert_equal 1, Order.number_shipped_since(now - 2 * week_in_secs)
  end

  # illustrates stubbing instance method for all instances of a class
  def test_should_calculate_value_of_unshipped_orders
    Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
    Order.any_instance.stubs(:shipped_on).returns(nil)
    Order.any_instance.stubs(:total_cost).returns(10)
    assert_equal 30, Order.unshipped_value
  end

end

Version data entries

89 entries across 76 versions & 9 rubygems

Version Path
floehopper-mocha-0.9.6.20090629164857 examples/stubba.rb
floehopper-mocha-0.9.6.20090629165308 examples/stubba.rb
floehopper-mocha-0.9.6.20090701111305 examples/stubba.rb
floehopper-mocha-0.9.7.20090701124354 examples/stubba.rb
jferris-mocha-0.9.5.0.1240002286 examples/stubba.rb
jferris-mocha-0.9.5.0.1240351621 examples/stubba.rb
jferris-mocha-0.9.5.0.1241126838 examples/stubba.rb
jferris-mocha-0.9.7.0.1247796736 examples/stubba.rb
jferris-mocha-0.9.7.20090701124354 examples/stubba.rb
jferris-mocha-0.9.7.20090911190113 examples/stubba.rb
challah-1.0.0.beta vendor/bundle/gems/mocha-0.10.5/examples/stubba.rb
mocha-0.12.10 examples/stubba.rb
challah-0.9.1.beta.3 vendor/bundle/gems/mocha-0.10.5/examples/stubba.rb
devise_sociable-0.1.0 vendor/bundle/gems/mocha-0.10.5/examples/stubba.rb
mocha-0.12.9 examples/stubba.rb
mocha-0.12.8 examples/stubba.rb
challah-0.9.1.beta vendor/bundle/gems/mocha-0.10.5/examples/stubba.rb
challah-0.9.0 vendor/bundle/gems/mocha-0.10.5/examples/stubba.rb
challah-rolls-0.2.0 vendor/bundle/gems/mocha-0.12.3/examples/stubba.rb
challah-rolls-0.2.0 vendor/bundle/gems/mocha-0.12.2/examples/stubba.rb