5. Automated Testing class Adder def add a, b a + b end end class AdderTest < Test::Unit::TestCase def test_adder adder = Adder . new assert_equal 4 , adder.add( 2 , 2 ) assert_equal 2 , adder.add( 4 , - 2 ) end end
6. Are You Really Testing Your Code? class Adder def add a, b a + b end end class AdderTest < Test::Unit::TestCase def test_adder assert_equal 4 , 2 + 2 end end
11. State-Based class DongleTest < Test::Unit::TestCase def test_wibble # Set up test inputs dongle = Dongle . new dongle.addString( "foo" ) dongle.addRemoteResource( " http://foo.com/bar " ) # Exercise functionality under test dongle.wibble! # Verify results are as expected dongle.answer.should == 42 end end
15. More Descriptive Test Names class AdderTest < Test::Unit::TestCase def test_should_add_two_positive_numbers assert_equal 4 , Adder . new .add( 2 , 2 ) end def test_should_add_a_positive_and_a_negative_number assert_equal 2 , Adder . new .add( 4 , - 2 ) end end
16. Matchers describe "An adder" do it "should add two positive numbers" do Adder . new .add( 2 , 2 ).should == 4 end it "should add a positive and a negative number" do Adder . new .add( 4 , - 2 ).should == 2 end end
17. Generated Documentation $ spec -f s adder_spec.rb An adder - should add two positive numbers - should add a positive and a negative number Finished in 0.005493 seconds 2 examples, 0 failures