…so I downloaded the latest version of test-unit-mock from RAA:
[05-31 20:35 test-unit-mock Mock objects for Test::Uni…
Lib…/Devel… 0.02] and started playing with it.
I created a simple class named LightBulb which has instance methods
"on" and “off” which change its status. When an object of this class
comes into existance, the status is off. I want the methods “on” and
"off" to be in that order. Calling the method “on” on a lightbulb which
is already “on” does not bode well. Like wise for the method “off”.
Everything went fine until I tried using “strictCallOrder = true”.
I thought I would be able to use it for calls in a loop. But that is not
how it works.
Can anybody shed some light on how to acheive this elegantly?
···
class LightBulb
attr :status
def initialize
@status = :off
end
def on
if @status == :on
raise 'boom!'
else
@status = :on
puts 'ON’
end
1
end
def off
if @status == :off
raise 'baam!'
else
@status = :off
puts 'OFF’
end
0
end
end
tm = LightBulb.new
2.times {
p tm.on
p tm.off
}
puts ‘------ Mock Test Begins -------’
require 'test/unit’
require ‘test/unit/mock’
TmClass = Test::Unit::MockObject(LightBulb)
tmObj = TmClass.new
tmObj.setReturnValues(:on=>1,:off=>0,:status=>[true,false])
tmObj.setCallOrder(:on, :off)
tmObj.strictCallOrder = true
tmObj.activate
2.times {
p tmObj.on
p tmObj.off
}
tmObj.verify
#--------------------------------------------
Produces:
ON
1
OFF
0
ON
1
OFF
0
------ Mock Test Begins -------
1
0
1
0
Loaded suite C:/atest/tst_mock
Started
Finished in 0.0 seconds.
0 tests, 0 assertions, 0 failures, 0 errors
C:/ruby/lib/ruby/site_ruby/1.8/test/unit/assertions.rb:31:in assert_block': Call order assertion failed: Unexpected method :on called from C:/atest/tst_mock.rb:57 at 0.00000 (Test::Unit::AssertionFailedError) from C:/ruby/lib/ruby/site_ruby/1.8/test/unit/assertions.rb:29:in
_wrap_assertion’
from C:/ruby/lib/ruby/site_ruby/1.8/test/unit/assertions.rb:33:in
assert_block' from C:/ruby/lib/ruby/site_ruby/1.8/test/unit/mock.rb:285:in
verify’
from C:/atest/tst_mock.rb:60