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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
May i know is there any way to expect a variable that throw null reference exception? for an example,

public class class1
{

public struct structVariable
{
public int a;
public string b;
}
public int a;
public structVariable[] structArray;

public class1(){}

public void assignValue()
{
a = 5;
structArray = new structVariable[1];
structArray[0].a = 1;
structArray[0].b = "test";
}

public void doSomething(){}
}

public class class2
{
private class1 c1;
private int a;
private string b;

public class2
{
c1 = new class1();
}

public void doSomething()
{
a = c1.a;
by = c1.structArray[a].b;

c1.doSomething();
}
}

testing, used to test doSomething() under class2
[Test]
public void test()
{
MockManager.Init();
Mock mockClass1 = MockManager.Mock(typeof(class1));

class2 c2 = new class2();

mockClass1.ExpectCall("doSomething");

c2.doSomething();

MockManager.Verify();
}

Error
System.NullReferenceException : Object reference not set to an instance of an object.

[/u]
asked by clteh9 (5.3k points)

1 Answer

0 votes
there is not such function that expect variables and throw exception. The method i used to solve this problem is set the private fields by using objectState.
answered by clteh9 (5.3k points)
...