提问者:小点点

单元测试服务Moq安装程序未返回值[重复]


我正在尝试为我的服务层编写一个单元测试。 这就是我正在写的测试。 我正试图得到回应,我对嘲弄有意见。 当我调试SaveCustomerRecordTextFile.WriteToDiskCustomerRecordAsync方法时,返回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);
        }
    } 

共1个答案

匿名用户

.returns(Task.FromResult(It.Isany<;String>()));您不应该在返回语句Moq中使用It类-在安装程序的返回中使用Isany时会发生什么?

将其替换为所需的任何值,如String.Empty或smthn