devfunkd
devfunkd

Reputation: 3234

Unit Test Value

I am trying to unit test this code,

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

Here is my test,

    [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 = "[email protected]",
            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 = "[email protected]",
            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 = "[email protected]",
            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);
}

I have debugged and stepped through can't find the value null it is referring to, my city variable is correct, the appointment parameter is fully populated. Is there something missing I am not seeing?

This is the output error from the unit test:

System.ArgumentNullException : Value cannot be null. Parameter name: arguments

Here is the stacktrace:

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

Upvotes: 1

Views: 1982

Answers (1)

Rikard
Rikard

Reputation: 3869

To test that everyhing has been called correctly:

[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 = "[email protected]",
        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 = "[email protected]",
        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 = "[email protected]",
        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 the return value:

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

Upvotes: 2

Related Questions