Mar*_*ers 5 c# nunit unit-testing rhino-mocks iqueryable
在我正在进行的.net 3.5项目中,我正在为服务类编写一些测试.
public class ServiceClass : IServiceClass
{
private readonly IRepository _repository;
public ServiceClass(IRepository repository)
{
_repository = repository;
}
#region IServiceClass Members
public IEnumerable<ActionType> GetAvailableActions()
{
IQueryable<ActionType> actionTypeQuery = _repository.Query<ActionType>();
return actionTypeQuery.Where(x => x.Name == "debug").AsEnumerable();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
而且我很难搞清楚如何存根或嘲笑
actionTypeQuery.Where(x => x.Name == "debug")
Run Code Online (Sandbox Code Playgroud)
部分.
这是我到目前为止所得到的:
[TestFixture]
public class ServiceClassTester
{
private ServiceClass _service;
private IRepository _repository;
private IQueryable<ActionType> _actionQuery;
[SetUp]
public void SetUp()
{
_repository = MockRepository.GenerateMock<IRepository>();
_service = new ServiceClass(_repository);
}
[Test]
public void heres_a_test()
{
_actionQuery = MockRepository.GenerateStub<IQueryable<ActionType>>();
_repository.Expect(x => x.Query<ActionType>()).Return(_actionQuery);
_actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);
_service.GetAvailableActions();
_repository.VerifyAllExpectations();
_actionQuery.VerifyAllExpectations();
}
}
Run Code Online (Sandbox Code Playgroud)
[注意:班级名称已被更改以保护无辜者]
但是,这无法用System.NullReferenceException在
_actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
如何使用RhinoMocks模拟或存根IQueryable.Where函数并通过此测试?
如果我当前的设置不允许我模拟或存根IQueryable,那么给出一个合理的解释原因.
感谢您阅读这个长篇大论的问题.
不使用Rhino模拟,您可以创建一个List,然后在其上调用.AsQueryable().例如
var actionTypeList = new List<ActionType>() {
new ActionType {}, //put your fake data here
new ActionType {}
};
var actionTypeRepo = actionTypeList.AsQueryable();
Run Code Online (Sandbox Code Playgroud)
这至少会让你成为一个虚假的存储库,但它不会让你验证方法被调用.
Where是一种扩展方法,这不是由IQueriable接口实现的方法.看看IQueriable的成员:http://msdn.microsoft.com/en-us/library/system.linq.iqueryable_members.aspx
扩展方法是静态的,不能被模拟.IMO,没有必要嘲笑Where,因为它是语言的一部分.您应该只模拟存储库.
编辑,示例:
[TestFixture]
public class ServiceClassTester
{
private ServiceClass _service;
private IRepository _repository;
private IQueryable<ActionType> _actionQuery;
[SetUp]
public void SetUp()
{
_service = new ServiceClass(_repository);
// set up the actions. There is probably a more elegant way than this.
_actionQuery = (new List<ActionType>() { ActionA, ActionB }).AsQueryable();
// setup the repository
_repository = MockRepository.GenerateMock<IRepository>();
_repository.Stub(x => x.Query<ActionType>()).Return(_actionQuery);
}
[Test]
public void heres_a_test()
{
// act
var actions = _service.GetAvailableActions();
// assert
Assert.AreEqual(1, actions.Count());
// more asserts on he result of the tested method
}
}
Run Code Online (Sandbox Code Playgroud)
注意:您不需要期望调用,因为您的方法取决于模拟的返回值.如果它不会调用它,它将在断言上失败.这使您的测试更容易维护.
我最初想模拟像'IQueryable.Where(Func)'这样的调用,但我认为它是在错误的级别进行测试.相反,在我的测试中,我只是嘲笑了IQueryable,然后检查结果:
// Setup a dummy list that will be filtered, queried, etc
var actionList = new List<ActionType>()
{
new ActionType() { Name = "debug" },
new ActionType() { Name = "other" }
};
_repository.Expect(x => x.Query<ActionType>()).Return(actionList);
var result = _service.GetAvailableActions().ToList();
// Check the logic of GetAvailableActions returns the correct subset
// of actionList, etc:
Assert.That(result.Length, Is.EqualTo(1));
Assert.That(result[0], Is.EqualTo(actionList[0]);
_repository.VerifyAllExpectations();
Run Code Online (Sandbox Code Playgroud)