in

CodePrairie .NET

South Dakota .NET User Group

chrisortman

Mocking server objects from unit tests

So heres the problem statement:

I'd like to be able to use WatiN tests to test my MonoRail views. But the controllers that render these views depend heavily on calling an external webservice that is large (in terms of number of methods and amount of data returned).

Now I could setup a stub service and have my controllers call that, however I think that keeping track of the data it is supposed to return and when would be very difficult. If only I could create mock instances from my unit tests.

Hmm, maybe I can, take a look at this nugget:

MockRepository mocks = new MockRepository();
AccountControllerImpl mockController = mocks.PartialMock<AccountControllerImpl>();
using(WebServiceHost host = new WebServiceHost(mockController,new Uri("http://localhost:8901"))) {
 host.AddServiceEndpoint(typeof(IAccountController),new WebHttpBinding(), "Account.svc");
 host.Open();

 using (mocks.Record())
 {
  Expect.Call(mockController.Info("")).IgnoreArguments().Return(new XDocument(new XElement("Hello")));
 }
 using (mocks.Playback())
 {
  WatiN.Core.IE ie = new WatiN.Core.IE();
  ie.GoTo("http://localhost:4075/webhost/default.aspx");
  ie.Button(Find.ByValue("Submit")).Click();

  Assert.IsTrue(ie.ContainsText("Hello"));
 }            
}
 

I then tell my page I'm testing to hit the webservice at a different URL and viola it hits my mock service. To make this really usable I'd probably need to extend RhinoMocks to correctly generate the mock using only the interface (right now I have to use the Implementation class and mark it as InstanceContextMode = Single and have virtual methods)

Neat?

Published Aug 17 2007, 03:49 PM by chrisortman
Filed under: ,

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add
Powered by Community Server (Commercial Edition), by Telligent Systems