Create a gist now

Instantly share code, notes, and snippets.

@vkhorikov /1.cs
Last active Nov 1, 2017

What would you like to do?
Unit testing private methods
public class Customer
{
private CustomerStatus _status = CustomerStatus.Regular;
public void Promote()
{
_status = CustomerStatus.Preferred;
}
public decimal GetDiscount()
{
return _status == CustomerStatus.Preferred ? 0.05m : 0m;
}
}
public enum CustomerStatus
{
Regular,
Preferred
}
public class Order
{
private Customer _customer;
private List<Product> _products;
public string GenerateDescription()
{
return $"Customer name: {_customer.Name}, " +
$"total number of products: {_products.Count}, " +
$"total price: {GetPrice()}";
}
private decimal GetPrice()
{
decimal basePrice = /* Calculate based on _products */;
decimal discounts = /* Calculate based on _customer */;
decimal taxes = /* Calculate based on _products */;
return basePrice - discounts + taxes;
}
}
public class Order
{
private Customer _customer;
private List<Product> _products;
public string GenerateDescription()
{
var calculator = new PriceCalculator();
return $"Customer name: {_customer.Name}, " +
$"total number of products: {_products.Count}, " +
$"total price: {calculator.Calculate(_customer, _products)}";
}
}
public class PriceCalculator
{
public decimal Calculate(Customer customer, List<Product> products)
{
decimal basePrice = /* Calculate based on products */;
decimal discounts = /* Calculate based on customer */;
decimal taxes = /* Calculate based on products */;
return basePrice - discounts + taxes;
}
}
public class Order
{
public void CheckOut(OrderStatistics orderStatistics)
{
/* other work */
decimal totalAmount = /* calculate */;
orderStatistics.LogOrder(_customer.Id, totalAmount);
}
}
public class OrderStatistics
{
private List<LogEntry> _log;
public void LogOrder(int customerId, decimal amount)
{
_log.Add(new LogEntry(customerId, amount));
}
}
public class CheckOutHandler
{
public void Handle()
{
Order order = /* get an order */;
OrderStatistics orderStatistics = /* get order statistics */;
(int customerId, decimal amount) = order.CheckOut();
orderStatistics.LogOrder(customerId, amount);
}
}
public class Order
{
public (int customerId, decimal amount) CheckOut()
{
/* other work */
decimal totalAmount = /* calculate */;
return (_customer.Id, totalAmount);
}
}
public class OrderStatistics
{
private List<LogEntry> _log;
public void LogOrder(int customerId, decimal amount)
{
_log.Add(new LogEntry(customerId, amount));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment