I am new to unit testing (xUnit) and I am not quite sure how to approach this method when writing tests for it.
Basically I have a class called Label.cs and in the constructor I use DI to inject 2 interfaces.
I also have a Get() method where it:
- Constructs a XML request
- Removes any invalid characters
- Calls the API
- Reads the label info from the response
- Returns the response
Is it possible to write tests for a method like this? I understand that I can mock the dependencies, pass them into the constructor and setup the methods using Moq but what if I had 4 or 5 more validations methods from ILabelValidation inside the Get() method? Would I need to setup all those methods using Moq?
public class Label
{
private readonly ICarrierService _carrierService;
private readonly ILabelValidation _validator;
public Label(int accountId, ICarrierService carrierService, ILabelValidation validator)
{
_carrierService = carrierService;
_validator = validator;
}
public override async Task<CreateShipmentResponse> Get(int accountId, Shipment shipment)
{
string xmlRequest = ConstructRequest(shipment);
// strip out any invalid characters inside the request
xmlRequest = _validator.InvalidCharacterValidation(xmlRequest);
// what if I have another method that I use from the ILabelValidation?
// _validator.ValidatePackageCount(3);
var stringContent = new StringContent(xmlRequest, Encoding.UTF8, "text/xml");
// make the API request
var shipmentResponse = await _carrierService.CreateShipment(dict, stringContent);
var stream = await shipmentResponse.Content.ReadAsStreamAsync();
Models.Label labelInfo = GetLabelInfo(stream);
var response = new CreateShipmentResponse();
response.labels = new List<string>();
if (!string.IsNullOrEmpty(labelInfo.Base64String))
response.labels.Add(labelInfo.Base64String);
return response;
}
}