views:

601

answers:

1

I'm working on a generic repository and I would like to test it using a NUnit.Mocks . According to Mike Hadlow in his article you can do it using Rhino mocks like this:

User[] users = new User[] { };
...
Expect.Call(userRepository.GetAll()).Return(users);

So I thought maybe I could write the same thing in NUnit.Mocks like this :

dataProviderMock = new DynamicMock(typeof(IDataProvider<User>));
var user = new User {Username = "username", Password = "password"};
var users =new[]{ user };
dataProviderMock.ExpectAndReturn("GetAll",users);

but I'm getting an InvalidCastException as I expected since there's no way to cast an array of users to an IQueryable. So here's the question how can I mock an IQueryable using NUnit.Mocks?

A: 

It was easier than I thought :) There is this AsQueryable() extension method that lets convert an array to an IQueryable. So it doesn't matter if you are using Rhino Mocks or NUnit.Mocks. Here's what i did :

dataProviderMock = new DynamicMock(typeof(IDataProvider<User>));
var user = new User {Username = "username", Password = "password"};
var users =new[]{ user };
dataProviderMock.ExpectAndReturn("GetAll",users.AsQueryable());
Beatles1692