2

我有来自 AdventureWorks 数据库的下表,您可以在此处查看我的模型:

模型,

这是我的存储库服务

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;
        }
    }
}

这是我的测试课,我正在使用 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 变量为空。

这是来自异常的信息。

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
4

0 回答 0