Create a gist now

Instantly share code, notes, and snippets.

@vkhorikov /1.cs
Last active Mar 19, 2018

Embed
What would you like to do?
Code pollution
public class OrderRepository
{
public void Save(Order order)
{
/* ... */
}
public Order GetById(long id)
{
/* ... */
}
}
[Fact]
public void Some_integration_test()
{
// Arrange
var repository = new OrderRepository();
var service = new OrderService(repository);
long customerId = 42;
// Act
service.DoSomething(customerId);
// Assert
// ToDo : check there are 2 orders in the database for the customerId 42
}
[Fact]
public void Some_integration_test()
{
// Arrange
var repository = new OrderRepository();
var service = new OrderService(repository);
long customerId = 42;
// Act
service.DoSomething(customerId);
// Assert
IReadOnlyList<Order> orders = repository.GetByCustomerId(customerId); // Code added for the use in this unit test
/* validate orders */
}
public class Logger
{
public void Log(string text)
{
/* Log the text */
}
}
public class Controller
{
public void SomeMethod(Logger logger)
{
logger.Log("SomeMethod is called");
}
}
public class Logger
{
private readonly bool _isTestEnvironment;
public Logger(bool isTestEnvironment)
{
_isTestEnvironment = isTestEnvironment;
}
public void Log(string text)
{
if (_isTestEnvironment)
return;
/* Log the text */
}
}
public class Controller
{
public void SomeMethod(Logger logger)
{
logger.Log("SomeMethod is called");
}
}
[Fact]
public void Some_test()
{
// Arrange
var logger = new Logger(true);
var controller = new Controller();
// Act
controller.SomeMethod(logger);
// Assert
/* ... */
}
public interface ILogger
{
void Log(string text);
}
public class Logger : ILogger
{
public void Log(string text)
{
/* Log the text */
}
}
public class FakeLogger : ILogger
{
public void Log(string text)
{
}
}
public class Controller
{
public void SomeMethod(ILogger logger)
{
logger.Log("SomeMethod is called");
}
}
[Fact]
public void Some_test()
{
// Arrange
ILogger logger = new FakeLogger(); // FakeLogger instead of Logger
var controller = new Controller();
// Act
controller.SomeMethod(logger);
// Assert
/* ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment