1

我正在尝试对这段代码进行单元测试,

public bool IsCityAvailable(Appointment appointment)
{
    var city = _customerRepository.Find(appointment.CustomerId).City;
    return _employeeRepository.Get.Count(x => x.City == city) > 0;
 }

这是我的测试,

    [SetUp]
    public void Setup()
    {
        MockAppointmentRepository = new Mock<IRepository<Appointment>>();
        MockCustomerRepository = new Mock<IRepository<Customer>>();
        MockShiftRepository = new Mock<IRepository<Shift>>();
        MockEmployeeRepository = new Mock<IRepository<Employee>>();

        AppointmentService = new AppointmentService(MockCustomerRepository.Object, MockAppointmentRepository.Object, MockShiftRepository.Object, MockEmployeeRepository.Object);

        Customer = new Customer()
        {
            Address = "88 Taraview Road NE",
            City = "Calgary",
            Email = "charles.norris@outlook.com",
            FirstName = "Charles",
            LastName = "Norris",
            Id = 1,
            Phone = "587-888-8882",
            PostalCode = "X1X 1X1",
            Province = "AB"
        };

        Employee1 = new Employee()
        {
            Address = "12 Saddletowne Road NW",
            City = "Calgary",
            Email = "johnny.bravo@outlook.com",
            FirstName = "John",
            LastName = "Bravo",
            Id = 2,
            Phone = "403-999-2222",
            PostalCode = "X1X 1X1",
            Province = "AB"
        };

        Employee2 = new Employee()
        {
            Address = "12 Saddletowne Road NW",
            City = "Calgary",
            Email = "johnny.bravo@outlook.com",
            FirstName = "John",
            LastName = "Bravo",
            Id = 2,
            Phone = "403-999-2222",
            PostalCode = "X1X 1X1",
            Province = "AB"
        };

        Appointment = new Appointment()
        {
            Id = 1,
            Customer = Customer,
            CustomerId = Customer.Id,
            Employee = Employee1,
            EmployeeId = Employee1.Id,
            ScheduledTime = new DateTime(2013,10,15,18,30,00)
        };
    }

[Test]
public void IsCityAvailableShouldReturnAvailableWhenEmployeeInSameCityFound()
{
    // ARRANGE
    var employees = new List<Employee> { Employee1, Employee2 };

    var result = new Mock<IQueryable<Employee>>();
    result.Setup(r => r.GetEnumerator()).Returns(employees.GetEnumerator());

    MockCustomerRepository.Setup(x => x.Find(It.IsAny<int>())).Returns(Customer);
    MockEmployeeRepository.Setup(x => x.Get).Returns(result.Object);

    // ACT
    AppointmentService.IsCityAvailable(Appointment);

    // ASSERT
    MockCustomerRepository.Verify(x => x.Find(It.IsAny<int>()), Times.Once);
    MockEmployeeRepository.Verify(x => x.Get, Times.Once);
}

我已经调试并逐步完成找不到它所指的值 null,我的城市变量是正确的,约会参数已完全填充。有什么我没有看到的东西吗?

这是单元测试的输出错误:

System.ArgumentNullException :值不能为空。参数名称:arguments

这是堆栈跟踪:

System.ArgumentNullException : Value cannot be null.
Parameter name: arguments
   at System.Linq.Expressions.Expression.RequiresCanRead(Expression expression, String paramName)
   at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
   at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ref ReadOnlyCollection`1 arguments)
   at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
   at System.Linq.Queryable.Count(IQueryable`1 source, Expression`1 predicate)
   at Zenwire.Services.AppointmentService.IsCityAvailable(Appointment appointment) in AppointmentService.cs: line 81
   at Tests.Unit.Zenwire.Services.AppointmentServiceTests.IsCityAvailableShouldReturnAvailableWhenEmployeeInSameCityFound() in AppointmentServiceTests.cs: line 100
4

1 回答 1

2

要测试是否正确调用了所有内容:

[SetUp]
public void Setup()
{
    MockAppointmentRepository = new Mock<IRepository<Appointment>>();
    MockCustomerRepository = new Mock<IRepository<Customer>>();
    MockShiftRepository = new Mock<IRepository<Shift>>();
    MockEmployeeRepository = new Mock<IRepository<Employee>>();

    AppointmentService = new AppointmentService(MockCustomerRepository.Object, MockAppointmentRepository.Object, MockShiftRepository.Object, MockEmployeeRepository.Object);

    Customer = new Customer()
    {
        Address = "88 Taraview Road NE",
        City = "Calgary",
        Email = "charles.norris@outlook.com",
        FirstName = "Charles",
        LastName = "Norris",
        Id = 1,
        Phone = "587-888-8882",
        PostalCode = "X1X 1X1",
        Province = "AB"
    };

    Employee1 = new Employee()
    {
        Address = "12 Saddletowne Road NW",
        City = "Calgary",
        Email = "johnny.bravo@outlook.com",
        FirstName = "John",
        LastName = "Bravo",
        Id = 2,
        Phone = "403-999-2222",
        PostalCode = "X1X 1X1",
        Province = "AB"
    };

    Employee2 = new Employee()
    {
        Address = "12 Saddletowne Road NW",
        City = "Calgary",
        Email = "johnny.bravo@outlook.com",
        FirstName = "John",
        LastName = "Bravo",
        Id = 2,
        Phone = "403-999-2222",
        PostalCode = "X1X 1X1",
        Province = "AB"
    };

    Appointment = new Appointment()
    {
        Id = 1,
        Customer = Customer,
        CustomerId = Customer.Id,
        Employee = Employee1,
        EmployeeId = Employee1.Id,
        ScheduledTime = new DateTime(2013,10,15,18,30,00)
    };
}

[Test]
public void IsCityAvailableShouldReturnAvailableWhenEmployeeInSameCityFound()
{
// ARRANGE
MockCustomerRepository.Setup(x => x.Find(It.IsAny<int>())).Returns(Customer);
MockEmployeeRepository.Setup(x => x.Get).Returns(new List<Employee> { Employee1, Employee2 }.AsQueryable());

// ACT
AppointmentService.IsCityAvailable(Appointment);

// ASSERT
MockCustomerRepository.Verify(x => x.Find(It.IsAny<int>()), Times.Once);
MockEmployeeRepository.Verify(x => x.Get, Times.Once);
}

测试返回值:

[Test]
public void IsCityAvailableShouldReturnAvailableWhenEmployeeInSameCityFound()
{
// ARRANGE
MockCustomerRepository.Setup(x => x.Find(It.IsAny<int>())).Returns(Customer);
MockEmployeeRepository.Setup(x => x.Get).Returns(new List<Employee> { Employee1, Employee2 }.AsQueryable());

// ACT
var actual = AppointmentService.IsCityAvailable(Appointment);

// ASSERT
Assert.IsTrue(actual);
}
于 2013-10-18T22:26:09.707 回答