AdventureWorks从数据库中删除失败的测试

时间:2012-05-16 20:01:47

标签: c# unit-testing adventureworks argumentnullexception

我在AdventureWorks数据库中有以下表格,你可以在这里看到我的模型:

model

这是我的存储库服务

public class EmployeeRepository : IEmployeeRepository
{
    private AdventureWorksEntities dataBaseContext = new AdventureWorksEntities();

    public IQueryable <Employee> Employees
    {
        get
        {
            return dataBaseContext.Employees;
        }
    }

    public bool SaveEmployee(Employee employee)
    {
        try{
            dataBaseContext.Employees.AddObject(employee);
            dataBaseContext.SaveChanges();
            return true;
        }
        catch
        {
            return false;            
        }
    }

    public bool DeleteEmployee(int employeeId)
    {
        try
        {
            var employeeToDelete = from e in dataBaseContext.Employees
                                   where e.EmployeeID == employeeId
                                   select e as Employee;
            dataBaseContext.Employees.DeleteObject((Employee)employeeToDelete.FirstOrDefault());
            dataBaseContext.SaveChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }
}

这是我的Test类,我使用nunit进行测试。

[TestFixture]
class EmployeeRepositoryTest : BaseTestClass
{
    private Employee employee;
    [Test]
    public void Chech_if_repository_returns_non_null_number_of_Employees()
    {
        var obtainedEmployees = employeeRepository.Employees;
        Assert.Greater(obtainedEmployees.Count(),0);

    }

    [Test]
    public void Check_If_One_Can_Add_Employee()
    {
        employee = new Employee {EmployeeID = 3200, Title = "Mr"};
        success = employeeRepository.SaveEmployee(employee);
        if (success)
        {
            var obtainedEmployee = from e in employeeRepository.Employees
                                   where e.EmployeeID == employee.EmployeeID
                                   select e;
            Assert.AreEqual(obtainedEmployee.FirstOrDefault().EmployeeID,employee.EmployeeID);
            Assert.AreEqual(obtainedEmployee.FirstOrDefault().Title,employee.Title);

        }
    }

    [Test] //Problem is in delete section
    public void Check_If_One_Can_Delete_Employee()
    {
        employee = new Employee { EmployeeID = 3201, Title = "Ms" };
        employeeRepository.SaveEmployee(employee);

        success = employeeRepository.DeleteEmployee(employee.EmployeeID);
        Assert.IsTrue(success); //this is false
    }

    [Test]
    public void Check_If_One_Can_Save_Employee()
    {
        employee = new Employee { EmployeeID = 3202, Title = "Mr" };
        success = employeeRepository.SaveEmployee(employee);
        if (success)
        {
            employee.Title = "Mr";
            Assert.IsTrue(employeeRepository.SaveEmployee(employee));

            var obtainedEmployee = from e in employeeRepository.Employees
                                   where e.EmployeeID == employee.EmployeeID
                                   select e;
            Assert.AreEqual(obtainedEmployee.FirstOrDefault().EmployeeID, employee.EmployeeID);
            Assert.AreEqual(obtainedEmployee.FirstOrDefault().Title, employee.Title);

        }
    }

    [TearDown]
    public void Cleanup()
    {
        success = false;
        employeeRepository.DeleteEmployee(3200);
        employeeRepository.DeleteEmployee(32001);
        employeeRepository.DeleteEmployee(3202);


    }

}

我在System.ArgumentException方法中有一个DeleteEmployee。 employeeToDelete变量为null。

以下是来自例外的信息。

System.ArgumentNullException: Value cannot be null.
Parameter name: entity
   at System.Data.EntityUtil.ThrowArgumentNullException(String parameterName)
   at System.Data.EntityUtil.CheckArgumentNull[T](T value, String parameterName)
   at System.Data.Objects.ObjectContext.DeleteObject(Object entity, EntitySet expectedEntitySet)
   at System.Data.Objects.ObjectSet`1.DeleteObject(TEntity entity)
   at AdventureWorks.Domain.RepositoryServices.EmployeeRepository.DeleteEmployee(Int32 employeeId) in D:\dokumenty\Visual Studio 2010\Projects\Kainos\MVC3-Adventure-Works\AdventureWorks\AdventureWorks.Domain\RepositoryServices\EmployeeRepository.cs:line 45

0 个答案:

没有答案