Aaron
Aaron

Reputation: 4480

When using unit test variable in code turns up null

This is my first time writing a unit test in C# and I am writing a unit test on someone else's code. So please be nice in the responses. In the unit test I am calling on the tracer class and setting the voltage variable ctp.Voltagel (it is public) to 1. When I run a test the variable comes up as null. I am not sure why the state of the variable isn't staying at 1. Any ideas why? Here is my code. In the constructor of tracerCtrl, TracerParameters tp is declared as private. So I do not have access to it from the unit test. I need to set it in TracerParameters.

    [TestMethod]
    public void TestGetScanResultVoc()
    {
        TracerCtrl tracerCtrl;
        TracerParameters tp;
        int channel = 0;
        int dataSize = 500;
        int []data;
        data = new int[dataSize];
        int[] pyrn1Data = null;
        int result = 0;
        tracerCtrl = new TracerCtrl();
        tp = new tracerParameters();
        tp.Voltagel = 1;


        tracerCtrl.processData(data, dataSize, channel);
       }

The tracerCtrl Code

        public void processData(int[] data, int dataSize, int channel)
    {
        int i = 0;
        if (channel == tp.Voltagel)  //tp.Voltagel I get null
        {
            pyrn1Data = new int[dataSize];
            pyrn1DataSize = dataSize;
            for (i = 0; i < dataSize; i++)
            {
                pyrn1Data[i] = data[i];
            }
            bGotPyrn1 = true;
        }
    }

Upvotes: 0

Views: 232

Answers (2)

FLGMwt
FLGMwt

Reputation: 726

It seems that you instantiate tp but never tie it to tracerCtrl. You may have to do something like

tracerCtrl.Parameters = tp;

or if your the TracerCtrl has a constructor that takes a TracerParameters object,

tp = new tracerParameters();
tp.Voltagel = 1;
tracerCtrl = new TracerCtrl(tp);

EDIT: Didn't notice this before, but I'm curious about where tp is defined in the TracerCtrl code. I would expect the if statement to look more like:

if (channel == thisParameters.Voltagel)

If Parameters is indeed where the TracerCtrl's parameters are stored.

If you update your question with some more information about TracerCtrl I'd be happy to revise.

Upvotes: 0

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52117

The tp in processData cannot be the same variable as tp in TestGetScanResultVoc (which is a local variable, and you don't pass it as an argument).

Right-click on the tp in processData, then "Go to Declaration" to see where that variable actually came from. Then make sure you set the right variable.

Upvotes: 1

Related Questions