Mocking Sort With Mocha
Posted
by josephmate
on Stack Overflow
See other posts from Stack Overflow
or by josephmate
Published on 2010-04-06T23:38:23Z
Indexed on
2010/04/06
23:43 UTC
Read the original article
Hit count: 447
How can I mock an array's sort expect a lambda expression?
This is a trivial example of my problem:
# initializing the data
l = lambda { |a,b| a <=> b }
array = [ 1, 2, 3, 4, 5 ]
sorted_array = [ 2, 3, 8, 9, 1]
# I expect that sort will be called using the lambda as a parameter
array.expects(:sort).with( l ).returns( sorted_array )
# perform the sort using the lambda expression
temp = array.sort{|a,b| l.call(a,b) }
Now, at first I expected that this would work; however, I got the following error:
- expected exactly once, not yet invoked: [ 1, 2, 3, 4, 5 ].sort(#<Proc:0xb665eb48>)
Is there a way to do what I am looking for?
Cheers, Joseph
© Stack Overflow or respective owner