-
-
Save vkhorikov/aa5169347310b8e3fd446acca48e7336 to your computer and use it in GitHub Desktop.
Overriding methods in classes-dependencies
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 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; | |
} | |
} |
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 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}"; | |
} | |
} |
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
[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); | |
} |
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
new Mock<StatisticsCalculator> { CallBase = true }; // Preserves the base class behavior |
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 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); | |
} | |
} |
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 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