Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Coding

Ahmed Ali marzoukali

:octocat:
Coding
View GitHub Profile
View unittest-example-12.cs
[TestFixture]
public class PrintInvoiceCommandTests
{
private PrintInvoiceCommand _command;
private AutoMoqer _mocker;
private Invoice _invoice;
private const int InvoiceId = 1;
private const string UserName = "mrenze";
View unittest-example-11.cs
// 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();
}
View unittest-example-10.cs
// 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()
{
View unittest-example-9.cs
[TestFixture]
public class PrintInvoiceCommandTests
{
private PrintInvoiceCommand _command;
private AutoMoqer _mocker;
private Invoice _invoice;
private const int InvoiceId = 1;
private const string UserName = "mrenze";
View 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;
View unittest-example-7.cs
// 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
{
View unittest-example-6.cs
[TestFixture]
public class InvoiceWriterTests
{
private InvoiceWriter _writer;
private AutoMoqer _mocker;
private Invoice _invoice;
[SetUp]
public void SetUp()
{
View unittest-example-5.cs
public interface IInvoiceWriter
{
void Write(Invoice invoice);
}
public class InvoiceWriter : IInvoiceWriter
{
private readonly IPrinter _printer;
private readonly IPageLayout _layout;
View unittest-example-4.cs
// PrintInvoiceCommand Class
public class PrintInvoiceCommand
{
private readonly IDatabase _database;
private readonly IPrinter _printer;
public PrintInvoiceCommand(
IDatabase database,
View unittest-example-4.cs
[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);