I have a class where I implement a timer. I want to make xUnit tests for that class. When i try to run the tests i have the following error
System.NullReferenceException : Object reference not set to an instance of an object.
What i am doing in the constructor shouldn't fixed the specific error? Why not? Can someone explain to me why I got that error?
GuessingGameTimerTests.cs
private readonly GuessingGameTimer t;
public GuessingGameTimerTests(GuessingGameTimer t)
{
this.t = t;
}
[Fact]
public void StartTimerTest()
{
t.SetTimer(30000);
bool expected = t.IsEnabled();
Assert.True(expected);
}
....
GuessingGameTimer.cs
public class GuessingGameTimer
{
public event EventHandler OnNumberChanged;
private System.Timers.Timer NumberGeneratorTimer;
private int replacetime; // Time in seconds
private int reSetValue; // Time in seconds
//constractor starts the timer
public GuessingGameTimer(int replacetime)
{
this.replacetime = replacetime;
reSetValue = replacetime;
SetTimer(replacetime);
}
public void SetTimer(int replacetime)
{
NumberGeneratorTimer = new System.Timers.Timer(replacetime);
NumberGeneratorTimer.Elapsed += OnTick;
NumberGeneratorTimer.AutoReset = true;
NumberGeneratorTimer.Enabled = true;
this.replacetime = getSeconds();
reSetValue = getSeconds();
}
public void ResetTimer()
{
NumberGeneratorTimer.AutoReset = true;
NumberGeneratorTimer.Enabled = true;
replacetime = reSetValue;
}
public void StopTimer()
{
NumberGeneratorTimer.Enabled = false;
}
public int getSeconds()
{
return replacetime;
}
public Boolean IsEnabled()
{
return NumberGeneratorTimer.Enabled;
}