the payment” do credit_card = mock_model(CreditCard) result = mock(CreditCardGatewayResult) result.should_receive(:success?). and_return(true) gateway = mock(:gateway) Gateway.stub(:new => gateway) gateway.should_receive(:process).twice. and_return(result) credit_card.should_receive(:gateway). exactly(2).times.and_return(result) Subscription.stub_chain(:new, :add_data, :make_live, :check).and_return(true) customer = Customer.new customer.pay(credit_card) end it “fails when the gateway refuses” do ... end end end
the payment” do credit_card = mock_model(CreditCard) result = mock(CreditCardGatewayResult) result.should_receive(:success?). and_return(true) gateway = mock(:gateway) Gateway.stub(:new => gateway) gateway.should_receive(:process).twice. and_return(result) credit_card.should_receive(:gateway). exactly(2).times.and_return(result) Subscription.stub_chain(:new, :add_data, :make_live, :check).and_return(true) customer = Customer.new customer.pay(credit_card) end it “fails when the gateway refuses” do ... end end end The Verdict
the payment” do credit_card = mock_model(CreditCard) result = mock(CreditCardGatewayResult) result.should_receive(:success?). and_return(true) gateway = mock(:gateway) Gateway.stub(:new => gateway) gateway.should_receive(:process).twice. and_return(result) credit_card.should_receive(:gateway). exactly(2).times.and_return(result) Subscription.stub_chain(:new, :add_data, :make_live, :check).and_return(true) customer = Customer.new customer.pay(credit_card) end it “fails when the gateway refuses” do ... end end end 1. Look at all this setup code. 2. What’s going on with that stub_chain? 3. This kind of thing is duplicated all over this file, and the whole codebase. 4. Mocks cause lots of trouble. I need to start this test from scratch. The Verdict
the payment” do Gateway.stub(:process => true) card = CreditCard.new result = Customer.new.pay(card) result.should == SUCCESS end it “fails when the gateway refuses” do Gateway.stub(:process => false) ... end end end
the payment” do Gateway.stub(:process => true) card = CreditCard.new result = Customer.new.pay(card) result.should == SUCCESS end it “fails when the gateway refuses” do Gateway.stub(:process => false) ... end end end SOLVED
the payment” do Gateway.stub(:process => true) card = CreditCard.new result = Customer.new.pay(card) result.should == SUCCESS end it “fails when the gateway refuses” do Gateway.stub(:process => false) ... end end end 601 Bourbon St New Orleans LA 30492 29th Sep 2011 Dear Mr Ruby, Just a brief note to say thanks so much for sorting out my website. I’ll certainly be recommending you next time I hear of someone in a fix. Best wishes Jane SOLVED
1-516-403-2039. -- Tracey 1. Mocks and stubs can be a pain to set up. 2. Newer code is simpler and cleaner, but relies on everything working together correctly, and doesn’t catch all edge cases. 3. I must write more edge cases! The Verdict Customer CreditCard Gateway Subscription
“proceses the payment” do ... end it “fails when the gateway refuses” do ... end it “fails when the gateway times out” do ... end it “fails if the subscription isn’t created” do ... end ... end end
“proceses the payment” do ... end it “fails when the gateway refuses” do ... end it “fails when the gateway times out” do ... end it “fails if the subscription isn’t created” do ... end ... end end SOLVED
happy. You have her number. --Tracey 1. When testing a series of objects from just the outside point of view, you have millions of combinations. 2. You can’t keep up with the edge cases. 3. Tests are getting slower. 4. Therefore: ??? The Verdict Customer CreditCard Gateway Annual Monthly Subscription
In cases like these, someone is usually lying. The Witness describe Customer do describe “payment” do it “proceses the payment” do Gateway.stub(:process => true) card = CreditCard.new result = Customer.new.pay(card) result.should == SUCCESS end it “fails when the gateway refuses” do Gateway.stub(:process => false) ... end end end
The Autopsy In cases like these, someone is usually lying. The Witness describe Customer do describe “payment” do it “proceses the payment” do Gateway.stub(:process => true) card = CreditCard.new result = Customer.new.pay(card) result.should == SUCCESS end it “fails when the gateway refuses” do Gateway.stub(:process => false) ... end end end
= Customer.new(details) c.pay.should == true end end Customer Gateway Subscription describe Subscription do it “can be created” do c = Customer.new(details) s = Subscription.new(c) s.add_data(data) s.make_live s.verify.should == true end end
the payment” do credit_card = mock_model(CreditCard) result = mock(CreditCardGatewayResult) result.should_receive(:success?). and_return(true) gateway = mock(:gateway) Gateway.stub(:new => gateway) gateway.should_receive(:process).twice. and_return(result) credit_card.should_receive(:gateway). exactly(2).times.and_return(result) Subscription.stub_chain(:new, :add_data, :make_live, :check).and_return(true) customer = Customer.new customer.pay(credit_card) end it “fails when the gateway refuses” do ... end end end
the payment” do credit_card = mock_model(CreditCard) result = mock(CreditCardGatewayResult) result.should_receive(:success?). and_return(true) gateway = mock(:gateway) Gateway.stub(:new => gateway) gateway.should_receive(:process).twice. and_return(result) credit_card.should_receive(:gateway). exactly(2).times.and_return(result) Subscription.stub_chain(:new, :add_data, :make_live, :check).and_return(true) customer = Customer.new customer.pay(credit_card) end it “fails when the gateway refuses” do ... end end end “Well designed code is easy to test.” -- Corey Haines
the payment” do credit_card = mock_model(CreditCard) result = mock(CreditCardGatewayResult) result.should_receive(:success?). and_return(true) gateway = mock(:gateway) Gateway.stub(:new => gateway) gateway.should_receive(:process).twice. and_return(result) credit_card.should_receive(:gateway). exactly(2).times.and_return(result) Subscription.stub_chain(:new, :add_data, :make_live, :check).and_return(true) customer = Customer.new customer.pay(credit_card) end it “fails when the gateway refuses” do ... end end end Customer CreditCard Gateway Annual Monthly Subscription “Well designed code is easy to test.” -- Corey Haines
the payment” do credit_card = mock_model(CreditCard) result = mock(CreditCardGatewayResult) result.should_receive(:success?). and_return(true) gateway = mock(:gateway) Gateway.stub(:new => gateway) gateway.should_receive(:process).twice. and_return(result) credit_card.should_receive(:gateway). exactly(2).times.and_return(result) Subscription.stub_chain(:new, :add_data, :make_live, :check).and_return(true) customer = Customer.new customer.pay(credit_card) end it “fails when the gateway refuses” do ... end end end Customer CreditCard Gateway Annual Monthly Subscription “Well designed code is easy to test.” -- Corey Haines
not the same feud. Looks like I can use either mocks or stubs when it suits me. http://martinfowler.com/articles/ mocksArentStubs.html Pros and Cons 1. Isolation
not the same feud. Looks like I can use either mocks or stubs when it suits me. http://martinfowler.com/articles/ mocksArentStubs.html Pros and Cons 1. Isolation 2. Flexibility
not the same feud. Looks like I can use either mocks or stubs when it suits me. http://martinfowler.com/articles/ mocksArentStubs.html Pros and Cons 1. Isolation 2. Flexibility 3. Setup
not the same feud. Looks like I can use either mocks or stubs when it suits me. http://martinfowler.com/articles/ mocksArentStubs.html Pros and Cons 1. Isolation 2. Flexibility 3. Setup 4. Design
not the same feud. Looks like I can use either mocks or stubs when it suits me. http://martinfowler.com/articles/ mocksArentStubs.html Pros and Cons 1. Isolation 2. Flexibility 3. Setup 4. Design 5. Speed
the payment” do credit_card = mock_model(CreditCard) result = mock(CreditCardGatewayResult) result.should_receive(:success?). and_return(true) gateway = mock(:gateway) Gateway.stub(:new => gateway) gateway.should_receive(:process).twice. and_return(result) credit_card.should_receive(:gateway). exactly(2).times.and_return(result) Subscription.stub_chain(:new, :add_data, :make_live, :check).and_return(true) customer = Customer.new customer.pay(credit_card) end it “fails when the gateway refuses” do ... end end end Customer CreditCard Gateway Annual Monthly Subscription
PaymentGateway.stub( :take_payment => true) SubscriptionManager.stub( :create_for => true) it “creates the subscription” do SubscriptionManager.should_receive( :create_for(customer).and_return(true) customer = Customer.new customer.pay(credit_card) end it “takes payment” do PaymentGateway.should_receive( :take_payment).with(:credit_card). and_return(true) customer = Customer.new customer.pay(credit_card) end end end Customer PaymentService SubscriptionManager
PaymentGateway.stub( :take_payment => true) SubscriptionManager.stub( :create_for => true) it “creates the subscription” do SubscriptionManager.should_receive( :create_for(customer).and_return(true) customer = Customer.new customer.pay(credit_card) end it “takes payment” do PaymentGateway.should_receive( :take_payment).with(:credit_card). and_return(true) customer = Customer.new customer.pay(credit_card) end end end Customer PaymentService SubscriptionManager SOLVED