A sample example to set private variable to true from unit test class in C#.Net
//TestService.cs
public class TestService
{
private bool _isInitialized = false;
}
//TestServiceUnitTest.cs
using System.Reflection;
public class TestServiceUnitTest
{
private TestService _testService;
[TestInitalize]
private void testInitalize()
{
_testService = new TestService();
}
[TestMethod]
Private void SetInitializeToTrue()
{
FieldInfo field = typeof(TestService).GetField("_isInitialized", BindingFlags.NonPublic | BindingFlags.Instance);
}
//TestService.cs
public class TestService
{
private bool _isInitialized = false;
}
//TestServiceUnitTest.cs
using System.Reflection;
public class TestServiceUnitTest
{
private TestService _testService;
[TestInitalize]
private void testInitalize()
{
_testService = new TestService();
}
[TestMethod]
Private void SetInitializeToTrue()
{
FieldInfo field = typeof(TestService).GetField("_isInitialized", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(_testService , true);
}
![](https://lh6.googleusercontent.com/-ZhooVMgF1NU/Tm3n6aGfNOI/AAAAAAAABjk/kIqA07mZJ5Y/s800/dw-rp.png)
0 comments :
Post a Comment