-
-
Save vkhorikov/f82850f5b9b4ef0e571b825eea05a0b7 to your computer and use it in GitHub Desktop.
Unit testing private methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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