the user should be allowed to easily purchase our products Scenario: Purchases for more than 2500 should offer 20% Given the following products: | Name | Price | | MacBook Pro | 2000 | | ThinkPad | 1000 | When I add 1 "MacBook Pro" to my basket And I add 1 "ThinkPad" to my basket Then the final price should be 2600 Integration Saturday, August 11, 12
да може лесно да пазарува продукти Сценарий: Поръчките за повече от 2000 трябва да имат 20% отстъпка Дадено че има следните три продукта: | Име | Price | | MacBook Air | 2000 | | ThinkPad | 1000 | Когато добавя 1 "MacBook Air" в кошницата си И добавя 2 "ThinkPad" в кошницата си То цената трябва да е 2600 Integration Saturday, August 11, 12
do let(:registration) { double } before do Registration.stub :new => registration registration.stub :create => true end it "constructs a Registration with params[:registration]" do Registration.should_receive(:new).with('registration data') post :create, registration: 'registration data' end it "creates a registration" do registration.should_receive(:create) post :create end end end end Unit Saturday, August 11, 12
do let(:registration) { double } before do Registration.stub :new => registration registration.stub :create => true end it "constructs a Registration with params[:registration]" do Registration.should_receive(:new).with('registration data') post :create, registration: 'registration data' end it "creates a registration" do registration.should_receive(:create) post :create end end end end Saturday, August 11, 12
тестът да мине Правите подобрения 3 ...докато премахнете повторенията • Тествате кода, който бихте искали да имате • Няма да се компилира (липсващи методи/класове) • Пускате го и гледате как се проваля • Имате червен тест проверяващ функционалността Saturday, August 11, 12
тестът да мине Правите подобрения 3 ...докато премахнете повторенията • Добавяте достатъчно код за да мине теста • Нито ред повече • Най-простото решение, което ви хрумва • Имате работещ код и зелен тест, който го потвърждава Saturday, August 11, 12
do it "should support initialization" do message = Message.new('[email protected]', '[email protected]', 'Hi!') message.from.should == '[email protected]' message.to.should == '[email protected]' message.title.should == 'Hi!' end end F 1) NameError in 'Message should support initialization' uninitialized constant Message /work/message/spec/message_spec.rb:5: Finished in 0.009336 seconds 1 example, 1 failure Saturday, August 11, 12
:title def initialize(from, to, title) @from = from @to = to @title = title end end . Finished in 0.009999 seconds 1 example, 0 failures Saturday, August 11, 12
:to, :title def initialize(from, to, title) @from = from @to = to @title = title end def valid? @from =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end end .. Finished in 0.011689 seconds 2 examples, 0 failures Saturday, August 11, 12
:title def initialize(from, to, title) @from = from @to = to @title = title end def valid? @from =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end end Все още всичко е ОК Saturday, August 11, 12
:title def initialize(from, to, title) @from = from @to = to @title = title end def valid? @from =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ and @to =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end end ... Finished in 0.010058 seconds 3 examples, 0 failures Saturday, August 11, 12
@from = from @to = to @title = title end def valid? @from =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ and @to =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end end 1 2 3 Повторение Saturday, August 11, 12
@from = from @to = to @title = title end def valid? @from =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ and @to =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end private def email_valid?(address) address =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end end 1 2 3 Малки стъпки ... Finished in 0.010158 seconds 3 examples, 0 failures Saturday, August 11, 12
@from = from @to = to @title = title end def valid? email_valid?(@from) and @to =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end private def email_valid?(address) address =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end end 1 2 3 Ама наистина малки стъпки ... Finished in 0.010001 seconds 3 examples, 0 failures Saturday, August 11, 12
@from = from @to = to @title = title end def valid? email_valid?(@from) and email_valid?(@to) end private def email_valid?(address) address =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end end 1 2 3 Готово ... Finished in 0.009903 seconds 3 examples, 0 failures Saturday, August 11, 12
a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? — Brian Kenighan Saturday, August 11, 12