Sha256: 077da8525938eaaf5b30add90a60f73445542efb6d9601c98e3c3543332cd609

Contents?: true

Size: 1.2 KB

Versions: 89

Compression:

Stored size: 1.2 KB

Contents

require 'test/unit'
require 'mocha'

class MiscExampleTest < Test::Unit::TestCase
  
  def test_mocking_a_class_method
    product = Product.new
    Product.expects(:find).with(1).returns(product)
    assert_equal product, Product.find(1)
  end

  def test_mocking_an_instance_method_on_a_real_object
    product = Product.new
    product.expects(:save).returns(true)
    assert product.save
  end

  def test_stubbing_instance_methods_on_real_objects
    prices = [stub(:pence => 1000), stub(:pence => 2000)]
    product = Product.new
    product.stubs(:prices).returns(prices)
    assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
  end

  def test_stubbing_an_instance_method_on_all_instances_of_a_class
    Product.any_instance.stubs(:name).returns('stubbed_name')
    product = Product.new
    assert_equal 'stubbed_name', product.name
  end

  def test_traditional_mocking
    object = mock()
    object.expects(:expected_method).with(:p1, :p2).returns(:result)
    assert_equal :result, object.expected_method(:p1, :p2)
  end

  def test_shortcuts
    object = stub(:method1 => :result1, :method2 => :result2)
    assert_equal :result1, object.method1
    assert_equal :result2, object.method2
  end
  
end

Version data entries

89 entries across 76 versions & 9 rubygems

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