Created
July 1, 2017 16:48
-
-
Save marzoukali/25b11244da9e90f680743ff3a56f37a2 to your computer and use it in GitHub Desktop.
unittest-example-3
This file contains hidden or 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
[TestFixture] | |
public class PrintInvoiceCommandTests | |
{ | |
// Define the needed class and it's dependencies mocks | |
private PrintInvoiceCommand _command; | |
private Mock<IDatabase> _mockDatabase; | |
private Mock<IPrinter> _mockPrinter; | |
private Mock<IDateTimeWrapper> _mockDateTime; | |
private Invoice _invoice; | |
// Define the constants that we will use when setup the mocks | |
private const int InvoiceId = 1; | |
private const decimal Total = 1.23m; | |
private static readonly DateTime Date = new DateTime(2001, 2, 3); | |
[SetUp] | |
public void SetUp() | |
{ | |
_invoice = new Invoice() | |
{ | |
Id = InvoiceId, | |
Total = Total | |
}; | |
_mockDatabase = new Mock<IDatabase>(); | |
_mockPrinter = new Mock<IPrinter>(); | |
_mockDateTime = new Mock<IDateTimeWrapper>(); | |
// Setup the mocks behaviours | |
_mockDatabase | |
.Setup(p => p.GetInvoice(InvoiceId)) | |
.Returns(_invoice); | |
_mockDateTime | |
.Setup(p => p.GetNow()) | |
.Returns(Date); | |
_command = new PrintInvoiceCommand( | |
_mockDatabase.Object, | |
_mockPrinter.Object, | |
_mockDateTime.Object); | |
} | |
[Test] | |
public void TestExecuteShouldPrintInvoiceNumber() | |
{ | |
// Here we will called execute(...) then we will verify if the p.WriteLine("Invoice ID: 1") is called once and the invoice id is = 1 | |
_command.Execute(InvoiceId); | |
_mockPrinter | |
.Verify(p => p.WriteLine("Invoice ID: 1"), | |
Times.Once); | |
} | |
[Test] | |
public void TestExecuteShouldPrintTotalPrice() | |
{ | |
_command.Execute(InvoiceId); | |
_mockPrinter | |
.Verify(p => p.WriteLine("Total: $1.23"), | |
Times.Once); | |
} | |
[Test] | |
public void TestExecuteShouldPrintTodaysDate() | |
{ | |
_command.Execute(InvoiceId); | |
_mockPrinter | |
.Verify(p => p.WriteLine("Printed: 2/3/2001"), | |
Times.Once); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment