Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 117 lines (108 sloc) 5.95 KB
If we want, we can ask it to run just a particular test method:
$ ruby test_roman.rb -n test_range
or tests whose names match a regular expression:
$ ruby test_roman.rb -n /range/
This last capability is a great way of grouping your tests. Use meaningful names, and you’ll be
able to run (for example) all the shopping-cart-related tests by simply running tests with names
matching /cart/.
#Figure 13.1: Testing framework assertions
assert | refute(boolean, message )
Fails if boolean is (is not) false or nil.
assert_block { block }
Expects the block to return true.
assert_ | refute_ empty(collection, message )
Expects empty? on collection to return true (false).
assert_ | refute_ equal(expected, actual, message )
Expects actual to equal/not equal expected, using ==.
assert_ | refute_in_delta(expected_float, actual_float, delta, message )
Expects that the actual floating-point value is (is not) within delta of the expected value.
assert_ | refute_ in_epsilon(expected_float, actual_float, epsilon=0.001, message )
Calculates a delta value as epsilon * min(expected, actual) and then calls the _in_delta test.
assert_ | refute_ includes(collection, obj, message )
Expects include?(obj) on collection to return true (false).
assert_ | refute_ instance_of(klass, obj, message )
Expects obj to be (not to be) a instance of klass.
assert_ | refute_ kind_of(klass, obj, message )
Expects obj to be (not to be) a kind of klass.
assert_ | refute_ match(regexp, string, message )
Expects string to (not) match regexp.
assert_ | refute_ nil(obj, message )
Expects obj to be (not) nil.
assert_ | refute_ operator(obj1, operator, obj2, message )
Expects the result of sending the message operator to obj1 with parameter obj2 to be (not to be) true.
assert_raises(Exception, ...) { block }
Expects the block to raise one of the listed exceptions.
assert_ | refute_ respond_to(obj, message, message )
Expects obj to respond to (not respond to) message (a symbol).
assert_ | refute_ same(expected, actual, message )
Expects expected.equal?(actual).
assert_send(send_array, message )
Sends the message in send_array[1] to the receiver in send_array[0], passing the rest of send_array as arguments. Expects the return value to be true.
assert_throws(expected_symbol, message ) { block }
Expects the block to throw the given symbol.
flunk(message="Epic Fail!")
Always fails.
Indicates that a test is deliberately not run.
Always passes.
#Figure 13.2: Additional Test::Unit assertions
assert_not_equal(expected, actual, message )
Expects actual not to equal expected, using ==. Like refute_equal.
assert_not_match(regexp, string, message )
Expects string not to match regexp. Like refute_match.
assert_not_nil(obj, message )
Expects obj not to be nil. Like refute_nil.
assert_not_same(expected, actual, message )
Expects !expected.equal?(actual). Like refute_same.
assert_nothing_raised(Exception, ...) { block }
Expects the block not to raise one of the listed exceptions.
assert_nothing_thrown(expected_symbol, message ) { block }
Expects the block not to throw the given symbol.
assert_raise(Exception, ...) { block }
Synonym for assert_raises.
Figure 14.3: Debugger commands
b[reak] [file|class:]line Sets breakpoint at given line in file (default current file) or class.
b[reak] [file|class:]name Sets breakpoint at method in file or class.
b[reak] Displays breakpoints and watchpoints.
wat[ch] expr Breaks when expression becomes true.
del[ete] [nnn] Deletes breakpoint nnn (default all).
cat[ch] exception Stops when exception is raised.
cat[ch] Lists current catches.
tr[ace] (on|off) [all] Toggles execution trace of current or all threads.
disp[lay] expr Displays value of nnn every time debugger gets control.
disp[lay] Shows current displays.
undisp[lay] [nnn] Removes display (default all).
c[ont] Continues execution.
s[tep] nnn=1 Executes next nnn lines, stepping into methods.
n[ext] nnn=1 Executes next nnn lines, stepping over methods.
fin[ish] Finishes execution of the current function.
q[uit] Exits the debugger.
w[here] Displays current stack frame.
f[rame] Synonym for where.
l[ist] [start–end] Lists source lines from start to end.
up nnn=1 Moves up nnn levels in the stack frame.
down nnn=1 Moves down nnn levels in the stack frame.
v[ar] g[lobal] Displays global variables.
v[ar] l[ocal] Displays local variables.
v[ar] i[stance] obj Displays instance variables of obj.
v[ar] c[onst] Name Displays constants in class or module name.
m[ethod] i[nstance] obj Displays instance methods of obj.
m[ethod] Name Displays instance methods of the class or module name.
th[read] l[ist] Lists all threads.
th[read] [c[ur[rent]]] Displays status of current thread.
th[read] [c[ur[rent]]] nnn Makes thread nnn current and stops it.
th[read] stop nnn Makes thread nnn current and stops it.
th[read] resume nnn Resumes thread nnn.
th[read] [sw[itch]] nnn Switches thread context to nnn.
[p]expr Evaluates expr in the current context. expr may include assignment to variables and method invocations.
h[elp] Shows summary of commands.
empty A null command repeats the last command.
每运行完一个test method 就会回滚事务。
The data placed in the fixture files is automatically loaded
into the test database before each test.