Sinan AKYAZICI
Sinan AKYAZICI

Reputation: 3952

how can mocking two methods on depend in the class

interface ISample
{
    int Sum(int a,int b);
    int Multiplication(int x,int a,int b);
}

class Sample
{
    public int Sum(int a,int b)
    {
        return a+b;
    }

    public int Multiplication(int x,int a,int b)
   {
       return x*Sum(a,b);
   }
}

I want to test the Multiplication method. But To test the method of Multiplication , sum method need to mocking. How do i mock Sum method ? Is it possible that something like this?

Upvotes: 3

Views: 272

Answers (6)

adt
adt

Reputation: 4360

As other said you can use seperate interfaces . For Example

interface ISum{

   int Sum(int a, int b);
}

interface ISample
{
    int Multiplication(int x, int a, int b);
}

public class Sample : ISample
{
   ISum _sum;
    public Sample(ISum sum)
    {
      _sum = sum;
    }

   int Multiplication(int x, int a, int b)
   {
       return x*_sum.Sum(a,b);
   }
}

In your testing code:

[TestFixture]
public class TestSample
{
    [Test]
    public void TestSample()
    {
        var mockSum = new Mock<ISum>();
        mockSum.Setup(); // fill this what you want
        var sampleObj = new SampleObj(mockSum.Object);
        //Do Some Asserts
    }

}

Upvotes: 3

Paul
Paul

Reputation: 1875

Another suggestion is to implement the interface explicitly - then you will have to introduce the private functions which can be shared and tested for each method.

private int Sum(int a, int b)
{
    return a+b;
}

int ISample.Sum(int a,int b)
{
    return Sum(a,b);
}

int ISample.Multiplication(int x,int a,int b)
{
   return x*Sum(a,b);
}

Upvotes: 2

Lee
Lee

Reputation: 144136

Rather than trying to 'mock' the call to Sum in Multiplication you should simply test it independently since the call to Sum is an implementation detail. However, if your real code is somewhat more complicated, you could add an overload which takes a Func:

public int Multiplication(int x,int a,int b)
{
    return this.Multiplication(x, a, b, (a, b) => a + b);
}

public int Multiplication(int x,int a,int b, Func<int, int, int> sumFunc)
{
    return x * sumFunc(a, b);
}

You can then provide your own sumFunc instance in your tests.

Alternatively you could make Sum virtual and override it in your test methods that require it to be replaced with your own implementation.

Upvotes: 2

Erik Dietrich
Erik Dietrich

Reputation: 6090

The only tool that I've used that might accomplish what you're talking about is Moles. If you're using Moq, this cannot be done. But, either way, I'd suggest testing your class as a unit. That is, if you've written tests for Sum() and are satisfied with its behavior, move on to writing tests for Multipication() and, if they expose issues, fix them in Sum() or Multiplication, as needed.

Upvotes: 1

Marty
Marty

Reputation: 7522

You don't mock the class you are testing, you mock the classes that it depends on.

If you really want to be able to test the Multiplication() method independently of the Sum() method, you will have to separate it into its own interface, and provide the Sample class with an instance of that interface, which it can use to perform the sum.

An alternative is to simply test the Sum() method first. If the Sum() method passes all its tests, you can be reasonably sure that it works correctly. Once you know it works correctly, it is acceptable to build on that knowledge by testing the Multiplication() method.

Upvotes: 4

That Chuck Guy
That Chuck Guy

Reputation: 463

I would test Sum independently, and for the Multiplication tests assume that Sum works. If it works for the Sum tests, it should work for Multiplication too. Also, some would argue that how Multiplication does its thing shouldn't be known by the tests. What if you have a different algorithm later that doesn't depend on Sum?

Upvotes: 4

Related Questions