Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Last active March 20, 2018 12:30
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/aa5169347310b8e3fd446acca48e7336 to your computer and use it in GitHub Desktop.
Save vkhorikov/aa5169347310b8e3fd446acca48e7336 to your computer and use it in GitHub Desktop.
Overriding methods in classes-dependencies
public class StatisticsCalculator
{
public async Task<(double weightDelivered, double totalCost)> Calculate(int customerId)
{
List<DeliveryRecord> records = await GetDeliveries(customerId);
double weightDelivered = records.Sum(x => x.Weight);
double totalCost = records.Sum(x => x.Cost);
return (weightDelivered, totalCost);
}
public async Task<List<DeliveryRecord>> GetDeliveries(int id)
{
var client = new HttpClient();
string json = await client.GetStringAsync("http://external.provider.com/api/deliveries?customerId=" + id);
var result = JsonConvert.DeserializeObject<List<DeliveryRecord>>(json);
return result;
}
}
public class CustomerController
{
private readonly StatisticsCalculator _calculator;
public CustomerController(StatisticsCalculator calculator)
{
_calculator = calculator;
}
public async Task<string> GetStatistics(int customerId)
{
(double weightDelivered, double totalCost) = await _calculator.Calculate(customerId);
return $"Total weight delivered: {weightDelivered}. Total cost: {totalCost}";
}
}
[Fact]
public async Task No_delivery_records_form_correct_result()
{
// Arrange
var mock = new Mock<StatisticsCalculator> { CallBase = true };
mock.Setup(x => x.GetDeliveries(It.IsAny<int>()))
.Returns(Task.FromResult(new List<DeliveryRecord>()));
var controller = new CustomerController(mock.Object);
// Act
string result = await controller.GetStatistics(1);
// Assert
Assert.Equal("Total weight delivered: 0. Total cost: 0", result);
}
new Mock<StatisticsCalculator> { CallBase = true }; // Preserves the base class behavior
public class DeliveryGateway : IDeliveryGateway
{
public async Task<List<DeliveryRecord>> GetDeliveries(int customerId)
{
var client = new HttpClient();
string json = await client.GetStringAsync("http://external.provider.com/api/deliveries?customerId=" + customerId);
var result = JsonConvert.DeserializeObject<List<DeliveryRecord>>(json);
return result;
}
}
public class StatisticsCalculator
{
public (double weightDelivered, double totalCost) Calculate(List<DeliveryRecord> records)
{
double weightDelivered = records.Sum(x => x.Weight);
double totalCost = records.Sum(x => x.Cost);
return (weightDelivered, totalCost);
}
}
public class CustomerController
{
private readonly StatisticsCalculator _calculator;
private readonly IDeliveryGateway _gateway;
public CustomerController(StatisticsCalculator calculator, IDeliveryGateway gateway)
{
_calculator = calculator;
_gateway = gateway;
}
public async Task<string> GetStatistics(int customerId)
{
List<DeliveryRecord> records = await _gateway.GetDeliveries(customerId);
(double weightDelivered, double totalCost) = _calculator.Calculate(records);
return $"Total weight delivered: {weightDelivered}. Total cost: {totalCost}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment