犀牛模拟 - 剔除返回IQueryable(Of T)的东西

时间:2009-08-26 17:25:30

标签: linq unit-testing rhino-mocks

我经常使用Rhino Mocks 3.5,但最近遇到了一些我从未尝试过的东西。我想要一个服务并设置返回值 - 简单的东西真的

唯一的问题是,现在我的服务没有返回IList,而是返回IQueryable

所以当我尝试做这样的事情时 - 它会爆炸

<TestMethod()> _
    Public Sub Should_Populate_Users_Property_On_View_During_OnInit()
        Dim View As IUserView = MockRepository.GenerateStub(Of IUserView)()
        Dim Service As IUserService = MockRepository.GenerateStub(Of IUserService)()
        Dim Presenter As New UserPresenter(View, Service)

        Dim StubUserObjectCollection As New List(Of User)
        StubUserObjectCollection.Add(New User(1, "jdoe", "John", "Doe", 0, 0, 0, 1, 1))

        Service.Stub(Function(x) x.GetUserCollection()).[Return](StubUserObjectCollection)

        Presenter.OnViewInit()

        Assert.AreEqual(View.Users.Count, 1)
    End Sub

如何将服务存根以便为下面的单元测试启用(为简洁起见,保持简单)

Public Sub OnViewInit()
    Dim UserList As List(Of User) = mUserService.GetUserCollection.Where(Function(x) x.Active = 1).OrderBy(Function(x) x.FirstName).ToList()

    mView.Users = UserList
End Sub

1 个答案:

答案 0 :(得分:7)

你能不能将StubUserObjectCollection更改为IQueryable?

Dim SubList As New List(Of User)
StubList.Add(New User(1, "jdoe", "John", "Doe", 0, 0, 0, 1, 1))
Dim StubUserObjectCollection = StubList.AsQueryable();