Coming from Test::Unit to RSpec

RSpec’s expectation API is a superset of Test::Unit’s assertion API. Use this table to find the RSpec equivalent of Test::Unit’s asserts.

Test::Unit RSpec Comment
assert(object) N/A
assert_block {...} lambda {...}.call.should be_true
assert_equal(expected, actual) actual.should == expected Uses ==
’’ actual.should eql(expected) Uses Object.eql?
assert_in_delta(expected_float, actual_float, delta) actual_float.should be_close(expected_float, delta)
assert_instance_of(klass, object) actual.should be_an_instance_of(klass)
assert_match(pattern, string) string.should match(regexp)
’’ string.should =~ regexp
assert_nil(object) actual.should be_nil
assert_no_match(regexp, string) string.should_not match(regexp)
assert_not_equal(expected, actual) actual.should_not eql(expected) Uses !Object.eql?
assert_not_nil(object) actual.should_not be_nil
assert_not_same(expected, actual) actual.should_not equal(nil) Uses Object.equal?
assert_nothing_raised(args) {...} lambda {...}.should_not raise_error(Exception=nil, message=nil)
assert_nothing_thrown {...} lambda {...}.should_not throw_symbol(symbol=nil)
assert_operator(object1, operator, object2) N/A
assert_raise(args) {...} lambda {...}.should raise_error(Exception=nil, message=nil)
assert_raises(*args) {...} lambda {...}.should raise_error(Exception=nil, message=nil)
assert_respond_to(object, method) actual.should respond_to(method)
assert_same(expected, actual) actual.should equal(expected) Uses !Object.equal?
assert_send(send_array) N/A
assert_throws(expected_symbol, &proc) lambda {...}.should throw_symbol(symbol=nil)
flunk(message=”Flunked”) violated(message=nil)
N/A actual.should_be_something Passes if object.something? is true

Regarding Equality

RSpec lets you express equality the same way Ruby does. This is different from Test::Unit and other xUnit frameworks, and this may cause some confusion for those of you who are making a switch.

Consider this example:

words = "the words"
assert_equals("the words", words) #passes
assert_same("the words", words) #fails

xUnit frameworks traditionally use method names like #assert_equals to imply object equivalence (objects with the same values) and method names like #assert_same to imply object identity (actually the same object). For programmers who are used to this syntax, it makes perfect sense to see this in a unit testing framework for Ruby.

The problem that we found is that Ruby handles equality differently from other languages. In java, for example, we override Object#equals(other) to describe object equivalence, whereas we use == to describe object identity. In Ruby, we do just the opposite. In fact, Ruby provides us with four ways to express equality:

a == b
a === b
a.equal?(b)
a.eql?(b)

Each of these has its own semantics, which can (and often do) vary from class to class. In order to minimize the translation required to understand what an example might be expressing, we chose to express these directly in RSpec. If you understand Ruby equality, then you can understand what RSpec examples are describing:

a.should == b      #passes if a == b
a.should === b     #passes if a === b
a.should equal(b)  #passes if a.equal?(b)
a.should eql(b)    #passes if a.eql?(b)

See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.