Question

I am able to Mole WebRequest.CreateUri so that I can replace the hostname of the request with another. However, I would like to Mole Dns.GetAddrInfo instead so that I can keep the hostname the same, but make it so that the hostname resolves to another IP address.

In this way, I do not have to edit my hosts file for example as this is a less flexible approach.

Any advice?

Thank you.

Edit
I've uploaded a sample Visual Studio 2010 sln to the problem here:

When opening the sln,

  1. Click on the "Create Virtual Directory" button in the Service project properties' Web tab.
  2. Rebuild the solution
  3. Add the following entry to c:\windows\system32\drivers\etc\hosts:
    127.0.0.1 www.productionserver.com
  4. Run the TestMethod1 unit test in the Tests project.
    The unit tests should succeed.
  5. Remove the entry from the hosts file and re-run the unit test.
    It should fail.

I have looked at the implementation of System.ServiceModel.ClientBase<TChannel> using a decompiler and noticed that it eventually calls System.Net.Dns.GetAddrInfo to resolve the hostname to an IP address.

I am hoping this sln is simple enough to update and send back (or just comment what needs to be done and\or changed) so I can simply see what is required to solve the problem.

Thank you.

Edit 2
It seems this is not currently possible with Moles.

Was it helpful?

Solution

The System.Net stack generally can not be moled, as much of he functionality are outside the control of the managed code.

Instead, create an interface and stub in your production code. Use the interface with dependency injection, to pass the stub to the target code. This stub simply calls the System.Net methods you wish to intercept, during testing.

Moles will automatically generate a Stub type, from the interface. Simply instantiate the stub type, detour the desired methods/properties/events of the stub, and then pass the Stub type to the method being tested.

Using stubs and dependency injection works wherever Mole types can not be created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top