說明
puts: 顯示數據
raw: 將數據表轉換成陣列
diff!: 比較數據表
建立 test_board.feature
# test_board.feature
Feature: Test board
Background: Some backgrounds here
Scenario: Add row and column
Given a board like this:
| | 1 | 2 | 3 |
| 1 | | | |
| 2 | x | | |
| 3 | | | |
When player x plays in row 3, column 2
Then the board should look like this:
| | 1 | 2 | 3 |
| 1 | | | |
| 2 | x | | |
| 3 | | x | |
建立 steps.rb
# features/step_definitions/steps.rb
Given /^a board like this:$/ do |table|
@board = table.raw
end
When /^player x plays in row (\d+), column (\d+)$/ do |row, col|
row, col = row.to_i, col.to_i
@board[row][col] = 'x'
puts
end
Then /^the board should look like this:$/ do |expected_table|
expected_table.diff!(@board)
end