views:

875

answers:

2

Hi! I'm trying to use RhinoMock for mocking out a wcf service.

Say I have the following service:

    [OperationContract]
    List<User> SearchUsers(UserSearchFilter filter);

Adding this service with Visual Studio will generate a proxy, and that proxy has a Interface like:

 public interface ResourceService {

    System.IAsyncResult BeginSearchUsers(UserSearchFilter filter, System.AsyncCallback callback, object asyncState);

    ObservableCollection<User> EndSearchUsers(System.IAsyncResult result);
}

Then I create a ViewModel that uses this service, like this:

        private ResourceService service;
    public ViewModelBase(ResourceService serv)
    {
        service = serv;
        var filter = new UserSearchFilter();
        service.BeginSearchUsers(filter, a =>
        {
            this.Users = service.EndSearchUsers(a); 
        }, null);
    }

Then comes the question. How do I mock this service using RhinoMock?

       [TestMethod]
    public void UserGetsPopulatedOnCreationOfViewModel()
    {
        // Is stub the right thing to use?
        ResourceService serv = MockRepository.GenerateStub<ResourceService>();

        // Do some setup... Don't know how?
        var vm = new ViewModel(serv);

        Assert.IsTrue(vm.Users.Count > 0);
    }

I be really happy if someone could help me with the correct usage of RhinoMock

(Note: I'm using Silverlight, but I don't think that would change the way RhinoMock is used)

Thanks a lot!

+1  A: 

I would create interface that service would implement (IResourceService). Then on Silverlight side create custom implementation of IResourceService that calls WCF service itself.

RihnoMock would create the stub for IResourceService interface and not for WCF service.

It's very easy to do using Prism 2, you can read more here:

http://mokosh.co.uk/post/2009/04/19/prism-2-wpf-and-silverlight-services/

jarek
Hi Thanks for answering. Yes, I've tried that. (I guess this is what your suggesting: http://inquisitorjax.blogspot.com/2008/10/mocking-out-wcf-services-in-silverlight.html ) Problem is that I'm fairly new to mocking and RhinoMock, so I was unable to setup a Mock for the IResourceService that extended the autogenerated ResourceService. Do you have the syntax? Thanks again
Larsi
Looks like Brian below has described the same using Rhino Mocks. I'll look into that and try understand it. Thanks for your time.
Larsi
A: 

I wrote a 4-part article all about testing apps that use WCF services.

Part 2 talks about mocking out the service using RhinoMocks

Part 3 talks about mocking out an asynchronous service using Moq

Note that part 3 can be translated over to RhinoMocks very easily. I was just trying to show different mocking frameworks, and that the technique did not rely on the mocking framework.

Hope it helps!

EDIT So, in Rhino Mocks, you do this in the setup:

mockService.YourEvent += null;
IEventRaiser loadRaiser = LastCall.IgnoreArguments().GetEventRaiser();

Then in the playback, you do this:

loadRaiser.Raise(mockService, CreateEventArgs());

You can find more info on mocking events in Rhino in Phil Haack's blog post.

Brian Genisio
Hi, Brian, this is just what I need. Thanks. I'm fairly new to mocking, and I'm having truble translating from Moq to RhinoMock. What is the "CreatEventHandler" equal in RhinoMock? Thanks again.
Larsi
@Larsi: See the edit in my post.
Brian Genisio
@Brian. Thanks again. Nice code :-), thanks to Ayende too.
Larsi