* Get BDD on your objects
x = 13 * 4
x.should == 42
y = "hello"
y.length.should_not be(4)
* Use familiar syntax to specify things
# RSpec
"my string".should =~ /string/
lambda { raise "FAIL" }.should raise_error
# matchy
"my string".should =~ /string/
lambda { raise "FAIL" }.should raise_error
* Most of familiar RSpec Matchers are built in
# raise_error matcher
lambda {raise}.should raise_error #pass
lambda {raise MyCustomError.new}.should raise_error(MyCustomError) #pass
lambda {raise "message"}.should raise_error("message") #pass
lambda {raise "message"}.should raise_error(/essa/) #pass
# change matcher
lambda {@var+=1}.should change {@var}
# passes
lambda { }.should change {@var}
# fails
@var = 1
lambda {@var+=1}.should change {@var}.from(1).to(2)
# passes
# be_something matcher
@obj.should be_something
# passes if @obj.something? is true
* a lot more ...
* Get the speed of Test:Unit with the syntax of RSpec
* Create your own matchers
# nil_matcher (As a simple and quick example)
def_matcher :be_nil do |receiver, matcher, args|
receiver.nil?
end
nil.should be_nil # pass
nil.should_not be_nil # fails
'notnil'.should be_nil # fails
'notnil'.should_not be_nil # pass
# have(n).somethings matcher (A little bit more complex)
def_matcher :have do |receiver, matcher, args|
count = args[0]
something = matcher.msgs[0].name
actual = receiver.send(something).length
matcher.positive_msg = "Expected #{receiver} to have #{actual} #{something},
but found #{count} "
actual == count
end
class Thing
def widgets
@widgets ||= []
end
end
@thing.should have(3).widgetsVersion
1, updated 1047 days ago.
. o 0 (
edit |
history )