Created
June 9, 2009 04:39
-
-
Save jish/126271 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
# First of all, let's write a test | |
# | |
# A new board should be empty | |
class BoardTest < Test::Unit::TestCase | |
def test_a_new_board_should_be_empty | |
board = Board.new | |
assert_equal(0, board.size) | |
end | |
end | |
# Now, let's run the test. | |
# | |
# Here is the output: | |
# 1) Error: | |
# test_a_new_board_should_be_empty(BoardTest): | |
# NameError: uninitialized constant BoardTest::Board | |
# method test_a_new_board_should_be_empty in test.rb at line 9 | |
# | |
# 1 tests, 0 assertions, 0 failures, 1 errors | |
# The test doesn't know what the Board object is, let's define it: | |
class Board | |
end | |
class BoardTest < Test::Unit::TestCase | |
def test_a_new_board_should_be_empty | |
board = Board.new | |
assert_equal(0, board.size) | |
end | |
end | |
# ...and run the tests again: | |
# 1) Error: | |
# test_a_new_board_should_be_empty(BoardTest): | |
# NoMethodError: undefined method `size' for #<Board:0x49958> | |
# method test_a_new_board_should_be_empty in test.rb at line 35 | |
# | |
# 1 tests, 0 assertions, 0 failures, 1 errors | |
# Now we have a new error; undefined method 'size', let's define it as well: | |
class Board | |
def size | |
end | |
end | |
class BoardTest < Test::Unit::TestCase | |
def test_a_new_board_should_be_empty | |
board = Board.new | |
assert_equal(0, board.size) | |
end | |
end | |
# ...and run the tests again: | |
# 1) Failure: | |
# test_a_new_board_should_be_empty:60 | |
# <0> expected but was | |
# <nil>. | |
# | |
# 1 tests, 1 assertions, 1 failures, 0 errors | |
# Now, we have a failing test! This is what we want before we implement any new functionality. Since | |
# we have a failing test to begin with, once we make the test pass, we *know* that our app is | |
# working properly. | |
# | |
# Let's add some functionality to our app by making our test pass | |
class Board | |
def size | |
0 | |
end | |
end | |
class BoardTest < Test::Unit::TestCase | |
def test_a_new_board_should_be_empty | |
board = Board.new | |
assert_equal(0, board.size) | |
end | |
end | |
# WAIT!? That's wrong! | |
# That size method doesn't return the *size* of anything. It just returns zero. Every time! | |
# | |
# Yes, it does, you are very astute. Just bear with me for a moment. The important thing right now | |
# is that we added some functionality to our app by writing code *to make our test pass*. | |
# | |
# Let's see if it worked by running the tests again: | |
# 1 tests, 1 assertions, 0 failures, 0 errors | |
# Horray! The tests pass. Now adding new functionality to our app is a matter of writing another | |
# failing test... and making it pass. Don't worry, that size method will shape up in no time! | |
# | |
# Here is our new test: | |
class Board | |
def size | |
0 | |
end | |
end | |
class BoardTest < Test::Unit::TestCase | |
def test_a_new_board_should_be_empty | |
board = Board.new | |
assert_equal(0, board.size) | |
end | |
def test_should_be_able_to_initialize_a_new_board_of_a_specific_size | |
board = Board.new(5) | |
assert_equal(5, board.size) | |
end | |
end | |
# Let's run it and see what happens: | |
# 1) Error: | |
# test_should_be_able_to_initialize_a_new_board_of_a_specific_size(BoardTest): | |
# ArgumentError: wrong number of arguments (1 for 0) | |
# method initialize in test.rb at line 124 | |
# method new in test.rb at line 124 | |
# method test_should_be_able_to_initialize_a_new_board_of_a_specific_size in test.rb at line 124 | |
# | |
# 2 tests, 1 assertions, 0 failures, 1 errors | |
# An argument error. Our initialize method currently takes zero arguments, and we gave it one. | |
# No problem, we can fix that. | |
class Board | |
def initialize(size = 0) | |
end | |
def size | |
0 | |
end | |
end | |
class BoardTest < Test::Unit::TestCase | |
def test_a_new_board_should_be_empty | |
board = Board.new | |
assert_equal(0, board.size) | |
end | |
def test_should_be_able_to_initialize_a_new_board_of_a_specific_size | |
board = Board.new(5) | |
assert_equal(5, board.size) | |
end | |
end | |
# Now we're saying if we don't pass any arguments to initialize (Board.new) then set the parameter | |
# size to the value 0 (size = 0). | |
# | |
# Let's run the tests again and see what happens: | |
# 1) Failure: | |
# test_should_be_able_to_initialize_a_new_board_of_a_specific_size:162 | |
# <5> expected but was | |
# <0>. | |
# | |
# 2 tests, 2 assertions, 1 failures, 0 errors | |
# Sweet! Another failing test. Now we're ready to add some new functionality to our app. To do that | |
# we need to make this test pass, and be sure not to break any of our existing tests. | |
class Board | |
def initialize(size = 0) | |
@size = size | |
end | |
def size | |
@size | |
end | |
end | |
class BoardTest < Test::Unit::TestCase | |
def test_a_new_board_should_be_empty | |
board = Board.new | |
assert_equal(0, board.size) | |
end | |
def test_should_be_able_to_initialize_a_new_board_of_a_specific_size | |
board = Board.new(5) | |
assert_equal(5, board.size) | |
end | |
end | |
# Let's run the tests again: | |
# 2 tests, 2 assertions, 0 failures, 0 errors | |
# Nice. Our test suite consists of two tests that started out as failing tests. Now they're both | |
# passing, so we *know* that they they've added functionality to our app, and we *know* that our app | |
# is working right now. | |
# | |
# So, keep on adding failing tests then making them pass. Be sure not to break any of the existing | |
# tests. If you keep it up, you'll have an awesome feature-rich app in no time! | |
# | |
# Good luck! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment