Rhino Mocks – stub out something that returns IQueryable(Of T)

linqrhino-mocksunit-testing

I work w/ Rhino Mocks 3.5 a lot but recently came across something I had never tried before. I want to stub out a service and setup the return value – simple stuff really

The only issue is that now my service isn't returning IList, but instead IQueryable

So when I try to do something like this – it blows up

<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

How can I stub out the service to enable a unit test for the below (kept simple for brevity)

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

Best Solution

Can you not change your StubUserObjectCollection to be an 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();
Related Question