require'rubygems'require'spec'moduleSpyMatchersclassMethodRecorderdefmethod_missing(sym, *args)MethodVerifier.new({sym => args})
endenddefhave_receivedMethodRecorder.newendclassMethodVerifierdefinitialize(expected)@expected= expected
enddefmatches?(target)@target= target
@target.was_method_called?(@expected)
enddeffailure_message"expected #{@target.__subject__.inspect} to have received #{@expected.keys[0]}(#{@expected.values[0]})"enddefnegative_failure_message"expected #{@target.__subject__.inspect} not to have received #{@expected.keys[0]}(#{@expected.values[0]})"endendendclassSpydefself.on(subject)Spy.new(subject)
enddefinitialize(subject)@subject= subject
@method_calls= []
enddefwas_method_called?(method_with_args)return@method_calls.include?(method_with_args)
enddef__subject__@subjectenddefmethod_missing(sym, *args)@method_calls<< {sym => args}
@subject.send(sym, *args)
endend
describe "ubiquitous assertion syntax"doincludeSpyMatchers
it "should test state"do2.should==1+1end
it "should test an error"do
lambda {[].not_a_method }.shouldraise_error(NoMethodError)
end
it "should test behavior"do
array =Spy.on []
[1, 2].eachdo |number|
array << number
end
array.should have_received <<1
array.should have_received <<2endend