What should our test cases be?
def palindrome?(word)
word == word.reverse
end
require 'minitest/autorun'
class TestPalindrome < Minitest::Test
def test_palindrome
assert_true palindrome? ''
assert_true palindrome? 'c'
assert_false palindrome? 'car'
assert_true palindrome? 'racecar'
assert_false palindrome? nil
end
end
$ ruby test_palindrome.rb
Run options: --seed 65310
# Running:
E
Finished in 0.001653s, 604.7796 runs/s, 2419.1183 assertions/s.
1) Error:
TestPalindrome#test_palindrome:
NoMethodError: undefined method `reverse' for nil:NilClass
/Users/nick/testing-talk/palindrome.rb:2:in `palindrome?'
test_palindrome.rb:10:in `test_palindrome'
1 runs, 4 assertions, 0 failures, 1 errors, 0 skips
describe 'palindrome?' do
context 'given an empty string' do
it 'returns true' do
expect(palindrome? '').to be true
end
end
context 'given a string with only one character' do
it 'returns true' do
expect(palindrome? 'c').to be true
end
end
context 'given a longer non-palindrome' do
it 'returns true' do
expect(palindrome? 'car').to be false
end
end
context 'given a longer palindrome' do
it 'returns true' do
expect(palindrome? 'racecar').to be true
end
end
context 'given a null value' do
it 'returns false' do
expect(palindrome? nil).to be false
end
end
end
"Fake" objects that replace the real implementations of code
The process of writing test code before implementation code.
Similar to test driven development, but involving more process and abstraction. Tests (usually acceptance tests) are written to verify specific user stories as they are implemented.
Test frameworks that focus on heirarchal specifications (like RSpec) or executable user stories (like Cucumber) are designed for and frequently used for BDD.