public
Created

  • Download Gist
attacker.rb
Ruby
1 2 3 4
module Attacker
def attack(target)
end
end
character_attacks_another_character.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
class CharacterAttacksAnotherCharacter
attr_reader :attacker, :target
 
def initialize(attacker, target)
@attacker = attacker.extend(Attacker)
@target = target.extend(Target)
end
 
def execute
attacker.attack(target)
self
end
end
character_attacks_another_character_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 
require 'spec_helper'
 
describe CharacterAttacksAnotherCharacter do
before do
@luke = stub
@vader = stub
@attack = CharacterAttacksAnotherCharacter.new(@luke, @vader)
end
 
describe "constructor" do
it "should assign the attacker and the target" do
@attack.attacker.should be(@luke)
@attack.target.should be(@vader)
end
 
it "should give roles to actors" do
@attack.attacker.should be_an(Attacker)
@attack.target.should be_a(Target)
end
end
 
describe "#execute" do
# This example tests the implementation. How to test the behaviour instead?
it "should make the attacker to attack the target" do
@luke.should_receive(:attack).with(@vader)
@attack.execute
end
 
it "should expose the actors" do
@executed_attack = @attack.execute
@executed_attack.attacker.should be(@luke)
@executed_attack.target.should be(@vader)
end
end
end
target.rb
Ruby
1 2
module Target
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.