单元测试服务最小起订量设置不返回值
Unit testing service Moq setup not returning value
我正在尝试为我的服务层编写单元测试。
这是我正在写的测试。我正在尝试取回回复,但我遇到了嘲笑问题。当我调试 saveCustomerRecordTextFile.WriteToDiskCustomerRecordAsync 方法 return null 时,我不确定我做错了什么。
public class InvitationServiceTests
{
private readonly Mock<ITranformToCustomerDistanceRecord> _customerRecordFileReader;
private readonly Mock<ISaveInviteeRecord> _customerRecordFileOutputWriter;
private readonly Mock<ICustomerDistanceFromDublinOffice> _distanceFromDublinOffice;
private readonly Mock<ISaveCustomerRecord> _saveCustomerRecordTextFile;
public InvitationServiceTests()
{
_customerRecordFileReader = new Mock<ITranformToCustomerDistanceRecord>();
_customerRecordFileOutputWriter = new Mock<ISaveInviteeRecord>();
_distanceFromDublinOffice = new Mock<ICustomerDistanceFromDublinOffice>();
_saveCustomerRecordTextFile = new Mock<ISaveCustomerRecord>();
}
[Fact]
public void InvitationService_return_statusOk()
{
_saveCustomerRecordTextFile.Setup(c => c.WriteToDiskCustomerRecordAsync(It.IsAny<IFormFile>())).Returns(Task.FromResult(It.IsAny<string>()));
var invitationService = new InvitationService(_customerRecordFileReader.Object,
_customerRecordFileOutputWriter.Object,
_distanceFromDublinOffice.Object,
_saveCustomerRecordTextFile.Object);
var s = invitationService.InviteToDublinOfficeAsync(It.IsAny<IFormFile>());
}
代码实现:
public class InvitationService : IInvitationService
{
private readonly ITranformToCustomerDistanceRecord _customerRecordFileReader;
private readonly ISaveInviteeRecord _customerRecordFileOutputWriter;
private readonly ICustomerDistanceFromDublinOffice _distanceFromDublinOffice;
private readonly ISaveCustomerRecord _saveCustomerRecordTextFile;
public InvitationService(
ITranformToCustomerDistanceRecord customerRecordFileReader,
ISaveInviteeRecord customerRecordFileOutputWriter,
ICustomerDistanceFromDublinOffice distanceFromDublinOffice,
ISaveCustomerRecord saveCustomerRecordTextFile)
{
_customerRecordFileReader = customerRecordFileReader ?? throw new ArgumentNullException(nameof(customerRecordFileReader));
_customerRecordFileOutputWriter = customerRecordFileOutputWriter ?? throw new ArgumentNullException(nameof(customerRecordFileOutputWriter));
_distanceFromDublinOffice = distanceFromDublinOffice ?? throw new ArgumentNullException(nameof(distanceFromDublinOffice));
_saveCustomerRecordTextFile = saveCustomerRecordTextFile ?? throw new ArgumentNullException(nameof(saveCustomerRecordTextFile));
}
public async Task<InviteeResponse> InviteToDublinOfficeAsync(IFormFile file)
{
var filepath = await _saveCustomerRecordTextFile.WriteToDiskCustomerRecordAsync(file);
var customerRecords = _customerRecordFileReader.MappingFromTextFileToCustomerRecord(filepath);
var customerRecordsWithDistance = _distanceFromDublinOffice.TransformCustomerRecordToInviteeDistanceRecord(customerRecords);
return _customerRecordFileOutputWriter.WriteToDiskInviteeToOffice(customerRecordsWithDistance);
}
}
.Returns(Task.FromResult(It.IsAny<string>()));
你不应该在 return 语句中使用它 class
将其替换为您想要的任何值,例如 String.Empty 或 smthn
我正在尝试为我的服务层编写单元测试。 这是我正在写的测试。我正在尝试取回回复,但我遇到了嘲笑问题。当我调试 saveCustomerRecordTextFile.WriteToDiskCustomerRecordAsync 方法 return null 时,我不确定我做错了什么。
public class InvitationServiceTests
{
private readonly Mock<ITranformToCustomerDistanceRecord> _customerRecordFileReader;
private readonly Mock<ISaveInviteeRecord> _customerRecordFileOutputWriter;
private readonly Mock<ICustomerDistanceFromDublinOffice> _distanceFromDublinOffice;
private readonly Mock<ISaveCustomerRecord> _saveCustomerRecordTextFile;
public InvitationServiceTests()
{
_customerRecordFileReader = new Mock<ITranformToCustomerDistanceRecord>();
_customerRecordFileOutputWriter = new Mock<ISaveInviteeRecord>();
_distanceFromDublinOffice = new Mock<ICustomerDistanceFromDublinOffice>();
_saveCustomerRecordTextFile = new Mock<ISaveCustomerRecord>();
}
[Fact]
public void InvitationService_return_statusOk()
{
_saveCustomerRecordTextFile.Setup(c => c.WriteToDiskCustomerRecordAsync(It.IsAny<IFormFile>())).Returns(Task.FromResult(It.IsAny<string>()));
var invitationService = new InvitationService(_customerRecordFileReader.Object,
_customerRecordFileOutputWriter.Object,
_distanceFromDublinOffice.Object,
_saveCustomerRecordTextFile.Object);
var s = invitationService.InviteToDublinOfficeAsync(It.IsAny<IFormFile>());
}
代码实现:
public class InvitationService : IInvitationService
{
private readonly ITranformToCustomerDistanceRecord _customerRecordFileReader;
private readonly ISaveInviteeRecord _customerRecordFileOutputWriter;
private readonly ICustomerDistanceFromDublinOffice _distanceFromDublinOffice;
private readonly ISaveCustomerRecord _saveCustomerRecordTextFile;
public InvitationService(
ITranformToCustomerDistanceRecord customerRecordFileReader,
ISaveInviteeRecord customerRecordFileOutputWriter,
ICustomerDistanceFromDublinOffice distanceFromDublinOffice,
ISaveCustomerRecord saveCustomerRecordTextFile)
{
_customerRecordFileReader = customerRecordFileReader ?? throw new ArgumentNullException(nameof(customerRecordFileReader));
_customerRecordFileOutputWriter = customerRecordFileOutputWriter ?? throw new ArgumentNullException(nameof(customerRecordFileOutputWriter));
_distanceFromDublinOffice = distanceFromDublinOffice ?? throw new ArgumentNullException(nameof(distanceFromDublinOffice));
_saveCustomerRecordTextFile = saveCustomerRecordTextFile ?? throw new ArgumentNullException(nameof(saveCustomerRecordTextFile));
}
public async Task<InviteeResponse> InviteToDublinOfficeAsync(IFormFile file)
{
var filepath = await _saveCustomerRecordTextFile.WriteToDiskCustomerRecordAsync(file);
var customerRecords = _customerRecordFileReader.MappingFromTextFileToCustomerRecord(filepath);
var customerRecordsWithDistance = _distanceFromDublinOffice.TransformCustomerRecordToInviteeDistanceRecord(customerRecords);
return _customerRecordFileOutputWriter.WriteToDiskInviteeToOffice(customerRecordsWithDistance);
}
}
.Returns(Task.FromResult(It.IsAny<string>()));
你不应该在 return 语句中使用它 class
将其替换为您想要的任何值,例如 String.Empty 或 smthn