11. Unit Testing
class ShipTest
class Ship
def test_crew
attr_accessor :captain
ship = Ship.new
attr_accessor :maties
ship.captain = 1
ship.mateys = 20
def crew
captain + maties
assert_equal 21, ship.crew
end
end
end
end
15. Integration Testing
def test_attacking_other_ships
Given quot;another shipquot; do |ship|
@ship_to_enter = Ship.find(ship)
end
Given quot;my shipquot; do
Story: Attacking other ships @my_ship = Ship.find_by_captain(quot;Sparrowquot;)
As Captain Sparrow end
I attack another ship
So I can buy more rum Given /my $number_of_mateys maties/ do |number_of_mateys|
@ship_to_enter.attackers = number_of_mateys
end
Scenario: Attack first ship
Given another ship When quot;we attach the other shipquot; do
And my ship @my_ship.attacks(@ship_to_enter)
And my 20 mateys end
When we attack the other ship
Then 1 pirate dies Then /$pirates_lost pirate dies/ do |pirates_lost|
assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers
And we steel 1000 pieces of gold
end
Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold|
assert_equal pieces_of_gold, @my_ship.gold
end
end
16. Integration Testing
Story: Attacking other ships
As Captain Sparrow
I attack another ship
So I can buy more rum
Scenario: Attack first ship
Given another ship
And my ship
And my 20 mateys
When we attack the other ship
Then 1 pirate dies
And we steel 1000 pieces of gold
17. def test_attacking_other_ships
Given quot;another shipquot; do |ship|
@ship_to_enter = Ship.find(ship)
end
Given quot;my shipquot; do
@my_ship = Ship.find_by_captain(quot;Sparrowquot;)
end
Given /my $number_of_mateys maties/ do |number_of_mateys|
@ship_to_enter.attackers = number_of_mateys
end
When quot;we attach the other shipquot; do
@my_ship.attacks(@ship_to_enter)
end
Then /$pirates_lost pirate dies/ do |pirates_lost|
assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers
end
Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold|
assert_equal pieces_of_gold, @my_ship.gold
end
end
20. Fixtures
pirates.yml ships.yml
captain_jack_sparrow: black_pearl:
name: Jack Sparrow name: The Black Pearl
enemy: Royal Navy max_crew: 85
captain: captain_jack_sparrow
interceptor:
name: The Interceptor
max_crew: 150
captain: captain_jack_sparrow
21. Factories
Factory.sequence :pirate do |n|
quot;matey#{n}quot;
end
Factory.define :ship do |f| should quot;only find big shipsquot; do
f.name 'Pirateship' Factory(:ship, :max_crew => 500)
f.max_crew 100 Factory(:ship, :max_crew => 200)
f.captain { Factory.next(:pirate) }
end ships = Ship.big_ones
assert_equal 1, ships.size
end
- Factory Girl
- Machinist
- Object Daddy
- Foundry
- Fixjour