如何创建用于在存储库中添加项目的单元测试? [英] How to Create a Unit Test for Adding Items in a Repository?

查看:61
本文介绍了如何创建用于在存储库中添加项目的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 IUnitOfWork 接口,该接口封装了我的自定义存储库.我的自定义存储库又继承自 IRepository 界面.

I have an IUnitOfWork interface that encapsulates my custom repositories. My custom repositories in turn inherit from an IRepository interface.

// The class that I am attempting to unit test
// EmployeeBusiness.cs
private readonly IUnitOfWork _unitOfWork;

public EmployeeBusiness(IUnitOfWork unitOfWork)
{
    _unitOfWork = unitOfWork;
}


public EmployeeDto AddEmployee(EmployeeDto employeeDto)
{
    var employee = Mapper.Map<Employee>(employeeDto);

    if (employee == null) return null;

    _unitOfWork.Employees
        .Add(employee);

    _unitOfWork.Complete();

    return Mapper.Map<EmployeeDto>(employee); 
}

// IUnitOfWork interface
public interface IUnitOfWork : IDisposable
{
    IEmployeeRepository Employees { get; }

    void Complete();
}

// IEmployeeRepository interface
public interface IEmployeeRepository : IRepository<Employee> { }

// IRepository<T> interface
public interface IRepository<TEntity> where TEntity : class
{
    void Add(TEntity entity);

    // I have added other methods for simplicity
}

我正在尝试对AddEmployee()方法进行单元测试,因为出现此错误:

I am struggling with unit testing the AddEmployee() method because I am getting this error:

该模拟对象预期被调用一次,但被调用0次:uow => uow.Employees.Add(Employee) 配置的设置: IUnitOfWork uow => uow.Employees.Add(员工) 执行的调用: IRepository`1.Add(Employee)

Expected invocation on the mock once, but was 0 times: uow => uow.Employees.Add(Employee) Configured setups: IUnitOfWork uow => uow.Employees.Add(Employee) Performed invocations: IRepository`1.Add(Employee)

这是我的单元测试

[SetUp]
public void SetUp()
{
    _employeeDto = new EmployeeDto
    {
        FirstName = "John",
        LastName = "Smith",
        BirthDate = new DateTime(1965, 12, 31)
    };

    _employee = new Employee
    {
       FirstName = "John",
       LastName = "Smith",
       BirthDate = new DateTime(1965, 12, 31)
    };

    _unitOfWork = new Mock<IUnitOfWork>();

    Mapper.Initialize(cfg =>
    {
        cfg.AddProfile<EmployeeProfile>();
    });
}


[Test]
public void AddEmployee_WhenCalled_AddEmployeeToDatabase()
{
    _unitOfWork.Setup(uow => uow.Employees.Add(_employee));
    _employeeBusiness = new EmployeeBusiness(_unitOfWork.Object);

    _employeeBusiness.AddEmployee(_employeeDto);

    _unitOfWork.Verify(uow => uow.Employees.Add(_employee), Times.Once);
    _unitOfWork.Verify(uow => uow.Complete(), Times.Once);
}

推荐答案

我设法通过更改单元测试来使其工作

I have managed to get it to work by changing my unit test

[Test]
public void AddEmployee_WhenCalled_AddEmployeeToDatabase()
{
    _unitOfWork.Setup(uow => uow.Employees.Add(_employee));
    _employeeBusiness = new EmployeeBusiness(_unitOfWork.Object);

    var result = _employeeBusiness.AddEmployee(_employeeDto);

    //_unitOfWork.Verify(uow => uow.Employees.Add(_employee), Times.Once); <-- This did not work 

    _unitOfWork.Verify(uow => uow.Employees.Add(It.IsAny<Employee>()), Times.Once); // <-- After changing this to It.IsAny<Employee>() it worked 
    _unitOfWork.Verify(uow => uow.Complete(), Times.Once);
}

有人可以帮助我了解使用It.IsAny<Employee>()而不是_employee变量的区别吗?

Can anyone please help me understand the difference of using It.IsAny<Employee>() as opposed to the _employee variable?

更新

有关解释,请访问索林的答案.

这篇关于如何创建用于在存储库中添加项目的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆