Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created April 7, 2010 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floehopper/359014 to your computer and use it in GitHub Desktop.
Save floehopper/359014 to your computer and use it in GitHub Desktop.
class MyClass
def initialize(array)
@array = array
end
def do_stuff(&block)
@array.sort(&block)
end
end
class MyTest < Test::Unit::TestCase
def test_should_call_sort_on_array
array = [ 1, 2, 3, 4, 5 ]
my_object = MyClass.new(array)
array.expects(:sort)
my_object.do_stuff
end
def test_should_return_result_of_array_sort
array = [ 1, 2, 3, 4, 5 ]
my_object = MyClass.new(array)
sorted_array = [ 2, 3, 8, 9, 1]
array.stubs(:sort).returns(sorted_array)
assert_equal sorted_array, my_object.do_stuff
end
def test_should_call_sort_on_array_with_supplied_block
array = [ 1, 2, 3, 4, 5 ]
my_object = MyClass.new(array)
array.stubs(:sort).yields(1, 2)
yielded_values = []
my_object.do_stuff { |a, b| yielded_values << [a, b] }
assert_equal [[1, 2]], yielded_values
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment