我如何编写一个测试用例来执行Delete操作

本文关键字:测试用例 执行 Delete 操作 一个 何编写 | 更新日期: 2023-09-27 18:04:21

给出以下代码,这是'保存'测试用例-我将如何编写一个'删除'测试用例?

[Test]
        public void Testsavesassignment()
        {
            var sAssignment = new SAssignment()
            {
                DateCreated = DateTime.Now,
                DateUpdated = DateTime.Now,
                Department = 9000.ToString(),
                EmployeeId = 4342342
            };
            Status status = null;
            var assignment = this.m_personnelService.SaveSAssignment(sAssignment, out status);
            Assert.IsTrue(status.Success);
            Assert.AreEqual(sAssignment.EmployeeId, assignment.EmployeeId);
            Assert.AreEqual(sAssignment.EmployeeId, assignment.DateCreated);
            Assert.AreEqual(sAssignment.DateUpdated, assignment.DateUpdated);
            Assert.AreEqual(sAssignment.Department, assignment.Department);
            Assert.AreNotEqual(sAssignment.Id, assignment.Id);
        }

我如何编写一个测试用例来执行Delete操作

如果您希望能够测试Delete,则必须了解m_personnelService的底层实现。

这里是一个如何实现单元测试的演练,包括删除,使用实体框架:http://msdn.microsoft.com/en-us/library/ff714955.aspx(跳转到标题为"EF中心实现"的部分-测试是更多的部分)。

这可能有帮助…

    [Test]
    public void TestDeleteAssignment()
    {
        //add assignment
        var myAssignment = new SAssignment()
        {
            DateCreated = DateTime.Now,
            DateUpdated = DateTime.Now,
            Department = 9000.ToString(),
            EmployeeId = 4342342
        };
        Status addStatus = null;
        var assignment = this.m_personnelService.SaveSAssignment(myAssignment, out addStatus);
        Assert.IsTrue(addStatus.Success);
        var targetAssignmentId = assignment.Id;
        //possibility 1
        Status deleteStatus = null;
        var assignment2 = this.m_personnelService.DeleteSAssignment(targetAssignmentId, out deleteStatus);
        Assert.IsTrue(deleteStatus.Success); //or Assert.AreEqual(assignment2.Id, targetAssignmentId);

        //possibility 2
        Status deleteStatus = null;
        var assignment3 = this.m_personnelService.DeleteSAssignment(targetAssignmentId);
        var result = this.m_personnelService.GetSAssignment(targetAssignmentId);
        Assert.IsNull(result);
    }