I think it's a good practise to always return empty lists or arrays instead of null when a method comes up with no results to avoid null checks in the code.
Because Rhino Mocks returns the default value for an object, which is null for lists and arrays, a lot of times I have to either add the null checks back in or setup the mocks with expectations to return lists.
Is there a way to configure or extend Rhino Mocks with this behaviour?
var repositoryMock = MockRepository.GenerateMock<ICustomerRepository>();
IList<Customer> customers = repositoryMock.getCustomers();
Assert.IsNotNull(customers);
Assert.AreEqual(0, customers.Count );
Turns out that this behaviour is possible with Moq as long as the returned object is IEnumerable. The following tests pass:
[Test]
public void EmptylListTest()
{
var repositoryMock = new Mock<ICustomerRepository>();
IEnumerable<Customer> customers = repositoryMock.Object.GetCustomers();
Assert.IsNotNull(customers);
Assert.AreEqual(0, customers.Count());
}
[Test]
public void EmptyArrayTest()
{
var repositoryMock = new Mock<ICustomerRepository>();
Customer[] customerArray = repositoryMock.Object.GetCustomerArray();
Assert.IsNotNull(customerArray);
Assert.AreEqual(0, customerArray.Length);
}
public interface ICustomerRepository
{
IEnumerable<Customer> GetCustomers();
Customer[] GetCustomerArray();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With