Skip to content

Instantly share code, notes, and snippets.

@rockarts
Created March 1, 2012 21:00
Show Gist options
  • Save rockarts/1953197 to your computer and use it in GitHub Desktop.
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.
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