I'm new to writing unit tests. I have used PrivateObject to access and modify the private member of a class to use it while testing.
However, if I want to access a private variable of the method that I'm testing, how can I do that. I'll not be changing any functionality, but I need to modify a variable in order to complete the unit test.
This is what I'm trying to do. In class ABC, I want to modify the dwConn as I will not be able to access the SqlConnectionManager for unit tests, but I'm able to access it when the application is running. Thanks so much for reading this. Any help reg this would be good.
public class ABCTest
{
public void MethodATest()
{
const string connectionString = @"Data Source=servername\MSSQLSERVER2008;Initial Catalog=DB1;User ID=user1;Password=pswd1";
SqlConnection sqlConn = new SqlConnection(connectionString);
PrivateObject param0 = new PrivateObject(target);
param0.SetField("dwConn", sqlConn);
actual = target.MethodA();
}
}
public Class ABC()
{
Method A()
{
SqlConnection dwConn = (SqlConnection)SqlConnectionManager.Instance.GetDefaultConnection();
using(dwconn)
{
//some stuff
}
}
}