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
Hi,

I have a Class “SystemManager” having constructor with three argument ServerName, UserName and Password. This SystemManager is derived from another Class “ConfigurationManager”. This ConfigurationManager created an instance of the “ConfigurationManagerConnection” class which will connect to a Server with Username/Password provided.

The class structure is as below.

Public Class SystemManager : ConfigurationManager
{
Public SystemManager(string serverName, string username, string password)
:base(serverName, username, password)
{
}
}

Public class ConfigurationManager
{
Private ConfigurationManagerConnection configurationManagerConnection;
Public ConfigurationManager(string serverName, string username, string password)
{
configurationManagerConnection = new ConfigurationManagerConnection(serverName, userName, siteCode);
}
}

Internal class ConfigurationManagerConnection
{
Public ConfigurationManagerConnection (string serverName, string username, string password)
{
//Code to connect to sccm server
}
}

Here in this case I want to Mock the ConfigurationManagerConnection class to avoid connection to the Sccm server.

var configurationManager = Isolate.Fake.Instance<ConfigurationManager>(Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored);
Isolate.Swap.NextInstance<ConfigurationManager>().With(configurationManager);
SystemManager target = new SystemManager(TestVariables.ServerName, TestVariables.UserName, TestVariables.Password);

I tried using the above lines of code but it still trying to connect to the Sccm server.

Anything which I missed?
asked by deepakkumarb (600 points)

1 Answer

0 votes
Hi,

From your code example it seems like you are trying to fake out the wrong type - ConfigurationManagerConnection contains the external dependency you want to isolate, but you try to fake ConfigurationManager. Here is how I rewrote the initialization code to make it pass:
var fakeConnection = Isolate.Fake.Instance<ConfigurationManagerConnection>();
Isolate.Swap.NextInstance<ConfigurationManagerConnection>().With(fakeConnection);

var mgr = new SystemManager("", "", "");


Please let me know if this works for you.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
...