1

Let's say I've a simple method which checks whether the passed number is Even & returns a boolean value. I'm new to mocking & trying it out. How can I mock this method using Moq framework?

public bool isEven(int x)
        {
            bool result = (x % 2 == 0) ? true : false;
            return result;
        }
Tim Smith
  • 72
  • 7
  • On a very side note, a simple ‘return (x % 2 == 0)’ will do – Davide Vitali Mar 03 '19 at 14:39
  • On another sidenote, the method name is confusing, it implies the method will return true if the number is Even or Odd. I'd name methods like this something like `IsEven` – oerkelens Mar 03 '19 at 14:42
  • are you using dependency injection to inject the instance of the class containing this method into whatever is calling it? what is calling this method and what are you attempting to test? – Dave M Mar 03 '19 at 14:44
  • 1
    Mark your method as `virtual`. – Crowcoder Mar 03 '19 at 14:47
  • This is just a sample method for trying, basically I wanted to know how exactly one can mock a method using a moq? – Tim Smith Mar 03 '19 at 14:50
  • 2
    Possible duplicate of [Using moq to mock only some methods](https://stackoverflow.com/questions/4769928/using-moq-to-mock-only-some-methods) – Crowcoder Mar 03 '19 at 14:54

1 Answers1

4

Let's assume that method is part of an interface:

public interface IMyInterface
{
   bool isEven(int x);
}

what you do to mock it is:

Mock<IMyInterface> myMock = new Mock<IMyInterface>();
myMock.Setup(x => x.isEven(It.Any<int>())).Returns(true);

What that means, it will setup the method in a way, that regardless to the value passed as argument, will return true.

You can then pass that Mock to other class / dependency like that:

var myNewObject = new Whateverclass(myMock.Object)

If your method lives in the concrete class, you can still do that but you need to mark your method as virtual. Rest of the process is exactly the same.

What Moq does underneath it creates an instance of its own class (like proxy) and put the return values for the members (methods, properties) that has been configured in setup methods.

EDIT:

Concrete example:

public class MyClass
{
   public virtual bool isEven(int x) {
     return (x % 2 == 0);
   }
}

what you do to mock it is:

Mock<MyClass> myMock = new Mock<MyClass>();
myMock.Setup(x => x.isEven(It.Any<int>())).Returns(true);

var myNewObject = new Whateverclass(myMock.Object)

And finally partial implementation:

public class MyClass
{
   public virtual bool isEven(int x) {
         return (x % 2 == 0);
       }
   public bool OtherMethod() {
       return !isEven();
   }
}

Mock<MyClass> myMock = new Mock<MyClass>();
myMock.CallBase = true;
myMock.Setup(x => x.isEven(It.Any<int>())).Returns(true);

var result = myMock.Object.OtherMethod();
madoxdev
  • 3,770
  • 1
  • 24
  • 39
  • So having an interface is must for mocking? – Tim Smith Mar 03 '19 at 15:00
  • not really. Its the most common used practice though. Take a look at the 2nd part of the answer, you can do also so called partial Mocks where you can mock only one method rather than everything. Depends on the needs, but process will be fairly similar. You will just replace the interface with concrete class in `<>` and rest should work. – madoxdev Mar 03 '19 at 15:02