whether it is empty" do stack.should be_empty stack.push 42 stack.should_not be_empty end it "should pop the items in reverse order" do stack.push 42 stack.push 5 stack.pop.should eq 5 stack.pop.should eq 42 lambda { stack.pop }.should raise_error(StackEmptyError) end end
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
да може лесно да пазарува продукти Сценарий: Поръчките за повече от 2000 трябва да имат 20% отстъпка Дадено че има следните три продукта: | Име | Price | | MacBook Air | 2000 | | ThinkPad | 1000 | Когато добавя 1 "MacBook Air" в кошницата си И добавя 2 "ThinkPad" в кошницата си То цената трябва да е 2600
MONEYZ N00BZ SHOULD BAI STUFF MISHUN: BAI STUFF CHEAPER I CAN HAS CHEEZEBURGERS: | NAME | MONEYZ | | MacBook Air | 2000 | | ThinkPad | 1000 | WEN I AD 1 "MacBook Air" TO MAI BASKET AN I AD 1 "ThinkPad" TO MAI BASKET DEN I SHOULD PAY 2600
end scenario 'should show existing quotes' do create_quote :text => 'The language of friendship', :author => 'Henry David Thoreau' login_as 'jdoe' visit '/' within('.quote') do page.should have_content('The language of friendship') page.should have_content('Henry David Thoreau') end end end
sequence(:faculty_number) { |n| "%05d" % n } full_name 'John Doe' end factory :admin, parent: :user do admin true end factory :topic do title 'Title' body 'Body' user end # ... end
тестът да мине Правите подобрения 3 ...докато премахнете повторенията • Тествате кода, който бихте искали да имате • Няма да се компилира (липсващи методи/класове) • Пускате го и гледате как се проваля • Имате червен тест проверяващ функционалността
тестът да мине Правите подобрения 3 ...докато премахнете повторенията • Добавяте достатъчно код за да мине теста • Нито ред повече • Най-простото решение, което ви хрумва • Имате работещ код и зелен тест, който го потвърждава
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
: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
:title def initialize(from, to, title) @from = from @to = to @title = title end def valid? @from =~ /^[a-z]+@[a-z]+(\.[a-z]+)+$/ end end Все още всичко е ОК
: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
@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 Повторение
@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
@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
@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
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