6

在我现在正在处理的 .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
}

我很难弄清楚如何存根或模拟

actionTypeQuery.Where(x => x.Name == "debug")

部分。

这是我到目前为止得到的:

[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();
    }

}

[注:类名已更改以保护无辜]

但这失败System.NullReferenceException

_actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);

所以我的问题是:

如何使用 RhinoMocks 模拟或存根 IQueryable.Where 函数并让此测试通过?

如果我当前的设置不允许我模拟或存根 IQueryable,那么请给出合理的解释。

感谢您阅读这个冗长的问题。

4

4 回答 4

6

在不使用 Rhino 模拟的情况下,您可以创建一个 List,然后在其上调用 .AsQueryable()。例如

var actionTypeList = new List<ActionType>() {
    new ActionType {},  //put your fake data here
    new ActionType {}
};

var actionTypeRepo = actionTypeList.AsQueryable();

这至少会给你一个假的存储库,但它不会让你验证方法是否被调用。

于 2009-04-26T17:52:15.180 回答
6

Where是扩展方法,这不是接口 IQueriable 实现的方法。查看 IQueriable 的成员:http: //msdn.microsoft.com/en-us/library/system.linq.iqueryable_members.aspx

扩展方法是静态的,不能被模拟。IMO,没有必要 mock 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
    }

}

注意:您不需要期待调用,因为您的方法取决于模拟的返回值。如果它不调用它,它将在断言上失败。这使您的测试更易于维护。

于 2009-04-27T00:26:58.990 回答
5

我最初也想模拟像“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();
于 2009-04-27T00:51:58.570 回答
0

我有类似的问题,我试图将谓词表达式存根到包含字符串的 Find-method,但 String 是引用类型,当然是不可变的。在我创建 testPredicate 之前没有成功,我将其传递给存根、实际 SUT 并最终断言。下面的代码有效。

    [Test()]
    public void Search_Apartment_With_Spesific_Address()
    {
        //ARRANGE
        var repositoryMock = MockRepository.GenerateMock<IApartmentRepository>();
        var notificationMock = MockRepository.GenerateMock<INotificationService>();
        var service = new ApartmentService(repositoryMock, notificationMock);
        var apartment = new List<Apartment> {new Apartment {Address = "Elm Street 2"}}.AsQueryable();

        Expression<Func<Apartment, bool>> testPredicate = a => a.Address == "Elm Street 2";
        repositoryMock.Stub(x => x.Find(testPredicate)).Return(apartment);

        //ACT
        service.Find(testPredicate);

        //ASSERT
        repositoryMock.AssertWasCalled(x => x.Find(testPredicate));
    }
于 2012-11-15T20:12:59.623 回答