iJay
iJay

Reputation: 4273

unit testing Method calling from another method in c#

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

Upvotes: 2

Views: 5844

Answers (5)

Myles McDonnell
Myles McDonnell

Reputation: 13335

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);
    }
}

Upvotes: 1

shenhengbin
shenhengbin

Reputation: 4294

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.

Upvotes: 1

Andrew Paradis
Andrew Paradis

Reputation: 36

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.

Upvotes: 2

seldary
seldary

Reputation: 6256

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.

Upvotes: 1

Erik A. Brandstadmoen
Erik A. Brandstadmoen

Reputation: 10588

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.

Upvotes: 3

Related Questions