0

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;
        }
Josh
  • 85
  • 1
  • 5
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Pavel Anikhouski Jul 14 '20 at 15:04
  • @PavelAnikhouski i dont understand why is null. i tried to do something like "private readonly GuessingGameTimer t = GuessingGameTimer(3000)" but still doesnt work – Josh Jul 14 '20 at 15:07
  • 1
    But then again you are assigning null explicitly in tests constructor. Remember dependency injection is not setup by default in unit tests project. So the constructor parameter will be null. – Durga Prasad Jul 14 '20 at 15:13

1 Answers1

0
    public GuessingGameTimerTests()
    {
        this.t = new GuessingGameTimer(3000);
    }
Durga Prasad
  • 815
  • 3
  • 13
  • tried it before but i got the following error "The following constructor parameters did not have matching fixture data" – Josh Jul 14 '20 at 15:19
  • can you post your complete test class? I cleaned up the constructor parameters as well in answer. Make sure your constructor does not take in any parameters. – Durga Prasad Jul 14 '20 at 15:28