Skip to content

Instantly share code, notes, and snippets.

View marzoukali's full-sized avatar
:octocat:
Coding

Ahmed Ali marzoukali

:octocat:
Coding
  • Dublin, Ireland
View GitHub Profile
@marzoukali
marzoukali / unittest-example-12.cs
Created July 9, 2017 21:29
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";
@marzoukali
marzoukali / unittest-example-11.cs
Created July 9, 2017 21:28
unittest-example-10
// The solution is to refactor the GOF class to be instance class and let it use interface to make it easy to inject
// Then use the IOC/DI singleton way to ensue using only one instance of the object.
// The Interface
public interface ISecurity
{
string GetUserName();
bool IsAdmin();
}
@marzoukali
marzoukali / unittest-example-10.cs
Created July 9, 2017 21:23
unittest-example-10
// This class will be used to create a simple GOF singletone object.
public class Security
{
private static Security _instance;
private string _userName;
private bool _isAdmin;
public static Security GetInstance()
{
@marzoukali
marzoukali / unittest-example-9.cs
Created July 3, 2017 11:04
unittest-example-9
[TestFixture]
public class PrintInvoiceCommandTests
{
private PrintInvoiceCommand _command;
private AutoMoqer _mocker;
private Invoice _invoice;
private const int InvoiceId = 1;
private const string UserName = "mrenze";
@marzoukali
marzoukali / unittest-example-8.cs
Created July 3, 2017 10:59
unittest-example-8.cs
// We solving the first issue by injected the real dependencies that the class depend on directly, and not injecting the whole
// ServiceLocator.
// And we solving the 2nd issue by implementing an IdentityService that we depend on to get the UserName directly.
public class PrintInvoiceCommand
{
private readonly IDatabase _database;
private readonly IInvoiceWriter _writer;
private readonly IIdentityService _identity;
@marzoukali
marzoukali / unittest-example-7.cs
Created July 3, 2017 10:55
unittest-example-7
// The Code which have issues before refactoring
// First: issue is passing the whole container which known as service locator pattern (Anti-Pattern) because it will
// let us lost the value of mocking the real dependencies that the class is depend on and also the Auto Moqing with a tool like AutoMoqer
// will be no longer available.
// Second: There is an issue in LastPrintedBy as been shown below we violating the the law of demeter by calling object that we depend on
// then from it we call another object then another which made the testing difficult specially if those classes has no interface to implement which
// may let us swap the real objects with mocks easly.
public class PrintInvoiceCommand
{
@marzoukali
marzoukali / unittest-example-6.cs
Created July 2, 2017 00:27
unittest-example-6
[TestFixture]
public class InvoiceWriterTests
{
private InvoiceWriter _writer;
private AutoMoqer _mocker;
private Invoice _invoice;
[SetUp]
public void SetUp()
{
public interface IInvoiceWriter
{
void Write(Invoice invoice);
}
public class InvoiceWriter : IInvoiceWriter
{
private readonly IPrinter _printer;
private readonly IPageLayout _layout;
@marzoukali
marzoukali / unittest-example-4.cs
Created July 1, 2017 23:38
unittest-example-4
// PrintInvoiceCommand Class
public class PrintInvoiceCommand
{
private readonly IDatabase _database;
private readonly IPrinter _printer;
public PrintInvoiceCommand(
IDatabase database,
@marzoukali
marzoukali / unittest-example-4.cs
Created July 1, 2017 16:50
unittest-example-4
[TestFixture]
public class PrintInvoiceCommandTestsWithAutoMoqer
{
private PrintInvoiceCommand _command;
private AutoMoqer _mocker;
private Invoice _invoice;
private const int InvoiceId = 1;
private const decimal Total = 4.50m;
private static readonly DateTime Date = new DateTime(2001, 2, 3);