2

Please Help me on this:

In here I need to call this Add(...) method through the Calling(...) method in MyAnotherClass. When I Assert it gives an error. Please show me the path.

public class MyClass
{
    public List<int> number = new List<int>();

    public void Add(int a, int b)
    {   
        int c = a + b;
        number.Add(c);
    }
 }

public class MyAnotherClass
{
    public void CallingMethod(int c, int d)
    {          
        MyClass mc = new MyClass();
        mc.Add( c, d);
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        MyAnotherClass mac = new MyAnotherClass();

        MyClass mc = new MyClass();

        mc.Add(2, 3);
        Assert.AreEqual(5, mc.number[0]);// **this work fine** 

        mac.CallingMethod(2, 3);
        Assert.AreEqual(5, mc.number[0]);// **but this not**
    }
}

Thanks

Begginer

1
  • Have you tried debugging it? What value does the second mc.number[0] provide? Commented Jan 6, 2012 at 13:51

5 Answers 5

3

You never update the object referenced by "mc" in the second case, but a new object with a reference called "mc" in the "MyAnotherClass" class.

Sign up to request clarification or add additional context in comments.

Comments

2

Your class, MyAnotherClass returns void, and does not provide a public property from which you can retrieve data from the inner MyClass.

You'd want something like:

public class MyAnotherClass
{
   public MyClass mc = new MyClass();
   public void CallingMethod(int c, int d)
   {
      mc.Add(c, d);
   }
}

Then, in your test class, you'd call mac.mc.number[0] in the comparison.

Comments

1

You are trying to access the "mc" object, defined in CallingMethod, outside of its scope. If MyAnotherClass were to get an instance of MyClass in its constructor (for example), reference it using a property, and then update it, and test this new reference - it would work.

Comments

1

I don't think the method makes any sense ,

or could you told me what it can do?

public void CallingMethod(int c, int d)
{          
    MyClass mc = new MyClass();
    mc.Add( c, d);
}

You'd better post your completely source for this method.

Or , if you can you could change the method like

public int CallingMethod(int c, int d)
{          
    MyClass mc = new MyClass();
    mc.Add( c, d);
    return mc.number[0];
}

Then , you can test it more easy.

Comments

1

You have two instances of MyClass, one you instantiate in the test, the other in MyAnotherClass. If you want to have only one instance you should pass it to the constructor of MyAnotherClass or make an argument of CallingMethod

public class MyAnotherClass
{
    private readonly MyClass _myClass;

    public MyAnotherClass(MyClass myClass)
    {
        _myClass = myClass;
    }

    public void CallingMethod(int c, int d)
    {          
        _myClass.Add( c, d);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.