do it 'destroys a planet' do martian = Martian.new planet = Planet.new martian.attack(planet) expect(planet).to be_destroyed end after { clean_up_space_junk } end
do it 'destroys a planet' do martian = Martian.new planet = Planet.new martian.attack(planet) expect(planet).to be_destroyed end after { clean_up_space_junk } end
do it 'destroys a planet' do martian = Martian.new planet = Planet.new martian.attack(planet) expect(planet).to be_destroyed end after { clean_up_space_junk } end
do it 'destroys a planet' do martian = Martian.new planet = Planet.new martian.attack(planet) expect(planet).to be_destroyed end after { clean_up_space_junk } end
do it 'destroys a planet' do martian = Martian.new planet = Planet.new martian.attack(planet) expect(planet).to be_destroyed end after { clean_up_space_junk } end
что мы тестируем DOC - depended-on component // зависимый компонент, необходим для теста describe Martian do it 'destroys a planet' do martian = Martian.new # SUT planet = Planet.new # DOC martian.attack(planet) expect(planet).to be_destroyed end after { clean_up_space_junk } end
Test Double (дублер), как обозначение для объекта, который заменяет реальный объект, от которого зависит SUT, в тестовых целях. Думайте о них, как о каскадёрах (Stunt Double)
name.blank? # ... end end describe Martian do it 'raises error without name' do name = '' weapon = nil # or any other thing expect { Martian.new(name, weapon) }.to raise_error(WhereIsMyName) end end
:storage def initialize @storage = {} end def get(key) storage[key] end def set(key, value) storage[key] = value end end class WeaponStorage attr_reader :storage def initialize(storage) @storage = storage end def find(name) storage.get(name) || 0 end def put(name, count) storage.set(name, count) end end
{ WeaponStorage.new(storage) } before { storage['blaster'] = 1 } it 'finds blaster' do expect(subject.find('blaster')).to eq 1 end it "doesn't find stone" do expect(subject.find('stone')).to eq 0 end end
weapon end def say "Hey! I'm armed with #{weapon.name}!" end end describe Martian do it 'says his weapon name' do weapon = double(:weapon, name: 'blaster') expect(Martian.new(weapon).say).to match(/blaster/) end end
do weapon = double(:weapon) expect(weapon.name).to eq(nil) #<Double :weapon> received unexpected message :name with (no args) end it 'short syntax' do weapon = double(:weapon, name: 'blaster') expect(weapon.name).to eq('blaster') end it 'long syntax' do weapon = double(:weapon) allow(weapon).to receive(:name).and_return('blaster') allow(weapon).to receive(:name2).and_raise(NoMethodError) expect(weapon.name).to eq('blaster') expect { weapon.name2 }.to raise_error(NoMethodError) end end
object method' do weapon = Weapon.new allow(weapon).to receive(:name).and_return('blaster') expect(weapon.name).to eq('blaster') end it 'can stub AR class' do martian = double(:martian, name: ‘Mark’) allow(Martian).to receive(:find) { martian } expect(martian.name).to eq(‘Mark’) end end
weapon end def shoot(target) weapon.shoot(target) end end describe Martian do it 'shoots' do weapon = spy(:weapon, shoot: 'Bang!') target = double(:target) martian = Martian.new(weapon) martian.shoot(target) expect(weapon).to have_received(:shoot).with(target) end end
Martian do it 'destroys a planet' do martian = Martian.new planet = double(:planet) expect(planet).to receive(:destroy!) martian.attack(planet) end end
def max_speed; World.speed_of_light; end end describe Warship it "knows it's position for certain time" do warship = Warship.new expect(World).to receive(:speed_of_light).and_return(10) expect(warship.position(10)).to eq(100) end end
world end def position(time); time * world.speed_of_light; end end describe Warship it "knows it's position for certain time" do world = double(:world, speed_of_light: 10) warship = Warship.new(world) expect(warship.position(10)).to eq(100) end end
world.speed_of_light; end end describe Warship do it "knows it's position for certain time" do warship = Warship.new warship.world = double(:world, speed_of_light: 10) expect(warship.position(10)).to eq(100) end end