如何编写单元测试用例以进行编辑功能?

时间:2018-04-20 05:45:53

标签: c# .net unit-testing moq edit

我必须测试编辑功能。我已经通过创建新对象测试了添加功能并将其传递给add方法。但是如何通过使用Id编辑它们来将这些添加的值传递给测试编辑功能。我使用模拟概念来测试添加功能。我需要传递已经增加的值来编辑方法,通过编辑来测试它。

添加功能的测试用例:

public void AddTote()         {

        var toteRep = new Mock<IEntityBaseRepository<Tote>>();

        var partToteRep = new Mock<IEntityBaseRepository<PartTote>>();

        var partRep = new Mock<IEntityBaseRepository<Part>>();

        var auditRep = new Mock<IEntityBaseRepository<Audit>>();

        var mockUnitOfWork = new Mock<IUnitOfWork>();
        mockUnitOfWork.Setup(uow => uow.Commit());
        var tote = new ToteDL(toteRep.Object, partToteRep.Object, partRep.Object, mockUnitOfWork.Object, auditRep.Object);
        ToteDTO toteDto = new ToteDTO();
        toteDto.Id = 1;
        toteDto.Name = "adds";
        toteDto.Description = "testing";
        Assert.AreEqual(true, tote.AddTote(toteDto).IsSuccess);

    }

编辑功能的测试用例:

public void EditTote()         {

        var toteRep = new Mock<IEntityBaseRepository<Tote>>();

        var partToteRep = new Mock<IEntityBaseRepository<PartTote>>();

        var partRep = new Mock<IEntityBaseRepository<Part>>();

        var auditRep = new Mock<IEntityBaseRepository<Audit>>();

        var mockUnitOfWork = new Mock<IUnitOfWork>();
        mockUnitOfWork.Setup(uow => uow.Commit());
        var tote = new ToteDL(toteRep.Object, partToteRep.Object, partRep.Object, mockUnitOfWork.Object, auditRep.Object);

        Assert.AreEqual(true, tote.EditTote(toteRep.Object).IsSuccess);

}

需要测试的编辑方法:

  public ResponseDTO EditTote(ToteDTO toteData)
    {
        ResponseDTO responseDTO = new ResponseDTO();
        try
        {
            var tote = this._toteRepository.GetSingle(toteData.Id);
            tote.Name = toteData.Name;
            tote.Description = toteData.Description;
            this._toteRepository.Edit(tote);
            audit = AuditMessage.GetAuditForFile(toteData.Name + " tote has been updated successfully.", (int)Enumerations.ModuleType.Tote, (int)Enumerations.OperationType.EditTote, 1);
            _auditRepository.Add(audit);
            this._unitOfWork.Commit();
            responseDTO.ResponseObject = tote;
            responseDTO.IsSuccess = true;
            responseDTO.UserMessage = tote.Name + " has been updated.";
        }

0 个答案:

没有答案