My TestInitialized class is invisible to my test

I'm trying to follow the advice of the sage Answermen re: moving my class instantiation to the TestInitialize method:

        [TestInitialize()]
        public void MyTestInitialize()
        {
            MessageClass target = new MessageClass();
        }

. . .

        [TestMethod()]
        public void SetMessageTypeSubcodeTest()
    ...
            target.SetMessageTypeSubcode(AMessageTypeSubcode); // <- here

...but I'm getting, "The name 'target' does not exist in the current context" above.

How can I make "target" visible to my test method?

Upvotes: 0

Views: 227

Answers (1)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38518

Your target object is defined in a local scope. Make it a field instead of a local variable so other class methods can access it.

class TestClass
{
    MessageClass _target;

    [TestInitialize()]
    public void MyTestInitialize()
    {
        _target = new MessageClass();
    }

    [TestMethod()]
    public void SetMessageTypeSubcodeTest()
    {
        _target.SetMessageTypeSubcode(AMessageTypeSubcode);
    }
}

Upvotes: 4

Related Questions