Create a gist now

Instantly share code, notes, and snippets.

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

What would you like to do?
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