如何使用 NUnit.Mocks 模拟通用存储库?
我正在开发 通用存储库 我想使用 NUnit.Mocks 来测试它。根据 Mike Hadlow 在他的文章中的说法,你可以使用像这样的 Rhino 模拟来做到这一点:
User[] users = new User[] { };
...
Expect.Call(userRepository.GetAll()).Return(users);
所以我想也许我可以在 NUnit.Mocks 中写同样的东西,如下所示:
dataProviderMock = new DynamicMock(typeof(IDataProvider<User>));
var user = new User {Username = "username", Password = "password"};
var users =new[]{ user };
dataProviderMock.ExpectAndReturn("GetAll",users);
但我得到了一个 InvalidCastException 正如我所期望的,因为没有办法进行强制转换IQueryable 的用户数组。 所以问题是如何使用 NUnit.Mocks 模拟 IQueryable?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这比我想象的要容易:)
有一个 AsQueryable() 扩展方法,可以将数组转换为 IQueryable。
因此,使用 Rhino Mocks 还是 NUnit.Mocks 并不重要。
这就是我所做的:
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 :