chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hello everyone,

Wanted to get your thoughts on this:

I have an asp.net web forms assembly with a number of classes, most of which access data access objects that thus require a database. What I'd like to be able to do is something like what NCover does, but take a step further to combine it with mock objects from a mocking framework.

Code story:

Given a class named WebApp.BrowseView, a method named Page_Load, and a local variable named objService, I want to place a "stand in" object for objService at runtime by shipping a mock object compiled using a mocking framework to the remote server.

Original WebApp.BrowseView.Page_Load assembly code:

// this hits a DB, taking time and not returning enough real data to test the UI fully
ItemService objService= new ItemService();
List<Item> items = objService.GetItems();
foreach (Item item in items)
{
...
}

NUnit test running on the test machine:

My fictional library that does all this is called "StuntMan"

[Test]
public void Grid_Shows_All_Possible_Options()
{

StuntMan.RemoteServer = "http://localhost/MyWebApp";
StuntMan.Connected = true; // attempts to connect, throws an exception if it fails.

...Imagine Selenium code or something else here to navigate to the web page...

Mock<ItemService> mockService = new Mock<ItemService>();
mockService .Expect(m => m.GetItems()).Callback() =>
return ... ));

StuntMan.PushStandIn("WebApp.BrowseView.Page_Load.objService", mockService);

Run Selenium assertions here...

StuntMan.PopStandIn();

}

This would then throw the StandIn off the stage and replace the original assembly. This is important because subsequent tests will need to direct their own stuntmen to replace the same service.

This would allow the stand in object to return whatever data I want in order to test the UI properly. I know that this is not a great practice to fake out running code, but it could solve a very real issue that people have for testing against unstable or hard to replicate data sources.

Of course, ASP.NET MVC is a great solution for this, but that is nice for new projects, but this is something many people need to do in legacy projects.

Is this something that TypeMock can help me with? Or, is there an Add-On or extension I would need to get or make?

Thank you,
Josh
asked by jgough (640 points)

3 Answers

0 votes
The problem you're going to run into here is that the unit test code, where you're setting up the expectations, is actually running in a different process than your web application, where your browser automation tests are running. To my knowledge, there's no way to do this - the unit test has no way to "remote in" to the web application appdomain and poke in the expectations.

That said, have you looked at Ivonna? It's an ASP.NET testing tool built on top of Isolator that lets you basically simulate an entire page lifecycle without having to do browser automation. That may do what you're looking for. That said, it doesn't do AJAX or client-side stuff because it's not actually running the stuff in a browser - it's all server-side.
answered by tillig (6.7k points)
0 votes
Thank you,

I thought this might be an issue, though I think it is technically feasible that this could be done, it would just require me learning how to write that. I know that NCover rewrites assemblies to instrument them, so if that is possible, it seems like this is possible, but it's pretty out of my league for the moment to figure out how to do that.

I will take a look at the tool you mentioned, though in this case what we're trying to do is really to test the browser interaction including javascript and postbacks and all that jazz.

Thanks again,
Josh
The problem you're going to run into here is that the unit test code, where you're setting up the expectations, is actually running in a different process than your web application, where your browser automation tests are running. To my knowledge, there's no way to do this - the unit test has no way to "remote in" to the web application appdomain and poke in the expectations.
answered by jgough (640 points)
0 votes
You could unit test server side code with Ivonna, and then fine tune your client code with WatiN or Selenium.
answered by ulu (1.7k points)
...