Created
March 1, 2012 21:00
-
-
Save rockarts/1953197 to your computer and use it in GitHub Desktop.
Port of http://gmoeck.github.com/2011/10/26/stubbing-is-not-enough.html to C# Asserts on the behaviour of an object instead of the state.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NUnit.Framework; | |
using Rhino.Mocks; | |
namespace ScratchPad | |
{ | |
[TestFixture] | |
public class TicketMachineTest | |
{ | |
[Test] | |
public void ShouldReserveTheNumberofTicketsInputtedWhenTheUserSubmitsARequest() | |
{ | |
TicketReserver reserver = MockRepository.GenerateMock<TicketReserver>(); | |
var machine = new TicketMachineInterface(reserver); | |
machine.NumberPressed(5); | |
machine.NumberPressed(5); | |
machine.SubmitRequest(); | |
reserver.AssertWasCalled(t => t.Reserve(55)); | |
} | |
[Test] | |
public void ShouldRemoveExtraTicketIfDeleteIsPressed() | |
{ | |
TicketReserver reserver = MockRepository.GenerateMock<TicketReserver>(); | |
var machine = new TicketMachineInterface(reserver); | |
machine.NumberPressed(5); | |
machine.NumberPressed(5); | |
machine.DeletePressed(); | |
machine.SubmitRequest(); | |
reserver.AssertWasCalled(t => t.Reserve(5)); | |
} | |
} | |
public class TicketMachineInterface | |
{ | |
private TicketReserver ticketReserver; | |
private string currentDisplay; | |
public TicketMachineInterface(TicketReserver ticketReserver) | |
{ | |
this.ticketReserver = ticketReserver; | |
currentDisplay = string.Empty; | |
} | |
public void NumberPressed(int number) | |
{ | |
currentDisplay += number.ToString(); | |
} | |
public void DeletePressed() | |
{ | |
currentDisplay = currentDisplay.Remove(currentDisplay.Length - 1); | |
} | |
public void SubmitRequest() | |
{ | |
ticketReserver.Reserve(int.Parse(currentDisplay)); | |
} | |
} | |
public class TicketReserver | |
{ | |
public virtual void Reserve(int numberOfTickets) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment