I am writing tests to test Infopath Forms to open in Form Control, my test method is as
[TestMethod]
public void Validate_OpenInfopathInFormControl()
{
Helper.OpenForm();
//Other Code
}
I have written Helper class as
public class Helper
{
public static void OpenForm()
{
//Code to Open Form
}
}
But everytime I execute this code, this gives me:
Test method InfoPathTest.TestAPI.Validate_OpenInfopathInFormControl threw exception: System.TypeInitializationException: The type initializer for 'InfoPathTest.Helpers.Helper' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object..
When I try to debug, it fails when Helper class needs to be initialized. This is really eating my head, is there any solution for this?
Here is the complete helper class:
namespace InfoPathTest.Helpers
{
public class Helper
{
//This is the form i need to OPEN
private static MainForm f = new MainForm();
private static bool _isOpen = false;
public static bool isOpen
{
set { _isOpen = value; }
get { return _isOpen; }
}
public static void OpenForm()
{
try
{
f.Show();
}
catch (Exception ex)
{
throw ex;
}
_isOpen = true;
}
public static void CloseForm()
{
f.Hide();
}
}
}
Typically, the TypeInitializationException exception reflects a catastrophic condition (the runtime is unable to instantiate a type) that prevents an application from continuing. Most commonly, the TypeInitializationException is thrown in response to some change in the executing environment of the application.
Type Initializers are a new language construct that allow for C# (and VB using similar syntax) to shortcut creating of custom constructors or writing additional code to initialize properties.
Your test calls Helper.OpenForm()
and as you have no static constructor, the only thing I can see that would cause the exception to be thrown is:
private static MainForm f = new MainForm();
Therefore something in the constructor for MainForm is likely throwing an exception. Put a breakpoint on the first line of the constructor for MainForm and step through until you see where the exception is thrown.
Alternatively you might find it easier to determine what the problem is, at least initially, by writing a new test you can step through that calls new MainForm()
directly:
[TestMethod]
public void Validate_OpenInfopathInFormControl()
{
var form = new MainForm();
}
Put a breakpoint on the only line of the test and step into the constructor to determine why it's throwing a NullReferenceException
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With