Reputation: 1568
update: 3
I am trying to mock out a method that a class uses to create new instances of other classes through unit of work. When I try and mock the method to return fixed data i get a null instead of the list when the getPage method has been called.
here is my code
[TestFixture()]
public class CustomerServiceTests
{
private ICustomerService service;
private IUnitOfWork mockUnitOfWork;
private IGenericRepository<Entities.Customer> repository;
private int customerId;
private int ContactId;
[SetUp()]
public void Setup()
{
customerId = 1;
ContactId = 1;
}
[Test()]
public void GetCustomers_should_return_three_results()
{
mockUnitOfWork = MockRepository.GenerateMock<IUnitOfWork>();
repository = MockRepository.GenerateMock<IGenericRepository<Entities.Customer>>();
List<Entities.Customer> customerList = new List<Entities.Customer>
{
new Entities.Customer { Id = 1, CompanyName = "test1", ContractorId = 1 },
new Entities.Customer { Id = 2, CompanyName = "test2", ContractorId = 2 },
new Entities.Customer { Id = 3, CompanyName = "test3", ContractorId = 1 },
new Entities.Customer { Id = 4, CompanyName = "test4", ContractorId = 1 },
new Entities.Customer { Id = 5, CompanyName = "test5", ContractorId = 4 }
};
var IQueryableList = customerList.AsEnumerable();
mockUnitOfWork.Stub(uow => uow.CustomerRepository).Return(repository);
repository.Stub(repo => repo.GetPaged()).Return(new ContentList<Entities.Customer> { List = IQueryableList, Total = customerList.Count });
service = new CustomerService(mockUnitOfWork);
var resultList = service.GetCustomers(new PageRequest {PageSize = 20, PageIndex = 1 });
var total = resultList.Data.Total;
Assert.AreEqual(10, total);
}
The part of the service code returns null instead of the list supplied.
customers = _service.CustomerRepository.GetPaged(filter, orderBy, pageRequest.PageSize, pageRequest.PageIndex, "CustomersContacts");
Upvotes: 3
Views: 804
Reputation: 1496
You set up a stub for GetPaged without parameters
GetPaged()
But you're calling GetPaged with parameters
GetPaged(filter, orderBy, pageRequest.PageSize, pageRequest.PageIndex, "CustomersContacts")
Try something like this (you'll need to verify the syntax, making sure it's the right types)
repository
.Stub(repo => repo.GetPaged(
Arg<string>.Is.Anything,
Arg<string>.Is.Anything,
Arg<int>.Is.Anything,
Arg<int>.Is.Anything,
Arg<string>.Is.Anything))
.Return(new ContentList<Entities.Customer> { List = IQueryableList, Total = customerList.Count });
Upvotes: 5