Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Last active November 1, 2017 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkhorikov/f82850f5b9b4ef0e571b825eea05a0b7 to your computer and use it in GitHub Desktop.
Save vkhorikov/f82850f5b9b4ef0e571b825eea05a0b7 to your computer and use it in GitHub Desktop.
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