Skip to content

Instantly share code, notes, and snippets.

@marzoukali
Created July 9, 2017 21:29
Show Gist options
  • Save marzoukali/c8bc2fcfe96e168e4ca64772d9ad28d9 to your computer and use it in GitHub Desktop.
Save marzoukali/c8bc2fcfe96e168e4ca64772d9ad28d9 to your computer and use it in GitHub Desktop.
unittest-example-12
[TestFixture]
public class PrintInvoiceCommandTests
{
private PrintInvoiceCommand _command;
private AutoMoqer _mocker;
private Invoice _invoice;
private const int InvoiceId = 1;
private const string UserName = "mrenze";
[SetUp]
public void SetUp()
{
_invoice = new Invoice();
_mocker = new AutoMoqer();
_mocker.GetMock<IDatabase>().
Setup(p => p.GetInvoice(InvoiceId))
.Returns(_invoice);
_mocker.GetMock<ISecurity>()
.Setup(p => p.IsAdmin())
.Returns(true);
_mocker.GetMock<ISecurity>()
.Setup(p => p.GetUserName())
.Returns(UserName);
_command = _mocker.Create<PrintInvoiceCommand>();
}
[Test]
public void TestExecuteShouldThrowExceptionIfUserIsNotAdmin()
{
_mocker.GetMock<ISecurity>()
.Setup(p => p.IsAdmin())
.Returns(false);
Assert.That(() => _command.Execute(InvoiceId),
Throws.TypeOf<UserNotAuthorizedException>());
}
[Test]
public void TestExecuteShouldPrintInvoice()
{
_command.Execute(InvoiceId);
_mocker.GetMock<IInvoiceWriter>()
.Verify(p => p.Print(_invoice),
Times.Once);
}
[Test]
public void TestExecuteShouldSetLastPrintedByToCurrentUser()
{
_command.Execute(InvoiceId);
Assert.That(_invoice.LastPrintedBy,
Is.EqualTo(UserName));
}
[Test]
public void TestExecuteShouldSaveChangesToDatabase()
{
_command.Execute(InvoiceId);
_mocker.GetMock<IDatabase>()
.Verify(p => p.Save(),
Times.Once);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment