RhinoMock Help: Mocking WCF Service

I am trying to use RhinoMock to bully a wcf service.

Let's say I have the following service:

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

Adding this service to Visual Studio will create a proxy server, and this proxy server has an interface:

  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, for example:

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

Then the question arises. How do I trick 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 am really happy if someone can help me with the proper use of RhinoMock

(Note: I am using Silverlight, but I do not think this will change the way RhinoMock is used)

Thanks a lot!

+4
source share
2 answers

I wrote an article 4 part of an article about testing applications that use WCF services.

Part 2 talks about bullying a service using RhinoMocks

Part 3 talks about bullying an asynchronous service using Moq

Please note that part 3 can be easily translated to RhinoMocks. I was just trying to show a different mocking framework, and this technique did not rely on a mocking structure.

Hope this helps!

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

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

Then during playback you do the following:

 loadRaiser.Raise(mockService, CreateEventArgs()); 

You can find more information on mocking events at Rhino's Phil Haak blog .

+4
source

I would create an interface that the service (IResourceService) will implement. Then, on the Silverlight side, create a custom IResourceService implementation that calls the WCF service itself.

RihnoMock will create a stub for the IResourceService interface, not for the WCF service.

This is very easy to do with Prism 2, you can read it here:

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

+1
source

All Articles