Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing Interfaces with Moq

I'm new to Moq and unit testing. I have been doing a unit test and this is the following code:

private Mock<IServiceAdapter> repository;

    [TestInitialize]
    public void Initialize()
    {
        repository= new Mock<IServiceAdapter>();
    }

[TestMethod()]
    public void SaveTest()
    {
        //Setup 
        string Name = "Name1"; 
        string Type = "1";
        string parentID = null;

        repository.Setup(x => x.Save(Name , Type, parentID)).Returns("Success").Verifiable();

        //Do
        var result = repository.Object.Save(Name , Type, parentID);
        //Assert
        repository.Verify();
    }

My problem is that the test will always return the string that I put in the Returns parameter, in other words, it will always return "success" or whatever I write in its place. I guess thats not right because thats not the real behavior of the service. Anyone knows how I can mirror the real behavior of the "Save" service I'm trying to test? So lets say, if the return string is different from the service method,then the test should fail.

Edited

The ServiceAdapter interface its just a wrapper for a Web Service which I call like a REST Service. It's a Web Forms Project.

I'm doing something like in this post

How to mock a web service

Should I create something like a FakeController with Dependency Injection to make it work?

like image 827
Morgan Soren Avatar asked Dec 10 '12 07:12

Morgan Soren


People also ask

Why do we use Moq in unit testing?

It is used in unit testing to isolate your class under test from its dependencies and ensure that the proper methods on the dependent objects are being called.

Can we use Moq in NUnit?

We will install NUnit and Moq using the Nuget package manager. Make sure that in your references, NUnit and Moq are present after installation: For running NUnit tests and exploring the tests, we need to install a visual studio extension called “NUnit 3 Test Adapter”.

Is Moq a testing framework?

The Moq framework is an open source unit testing framework that works very well with . NET code and Phil shows us how to use it.


1 Answers

You are testing mock here, which gives you nothing (because this mock is not used in your real application). In unit-testing you should create and test your real objects, which exist in your real application (i.e. interface implementations). Mocks used for mocking dependencies of objects under test.

So, mock of service adapter will be useful for tests of object, which uses that adapter, e.g. some controller tests:

private FooController _controller; // object under test, real object
private Mock<IServiceAdapter> _serviceAdapter; // dependency of controller

[TestInitialize]
public void Initialize()
{
    _serviceAdapter = new Mock<IServiceAdapter>();
    _controller = new FooController(_serviceAdapter.Object);
}

[TestMethod()]
public void SaveTest()
{
    // Arrange
    string name = "Name1"; 
    string type = "1";
    string parentID = null;

    _serviceAdapter.Setup(x => x.Save(name , type, parentID))
                   .Returns("Success").Verifiable();

    // Act on your object under test!
    // controller will call dependency
    var result =  _controller.Bar(name , type, parentID); 

    // Assert
    Assert.True(result); // verify result is correct
    _serviceAdapter.Verify(); // verify dependency was called
}
like image 146
Sergey Berezovskiy Avatar answered Sep 18 '22 20:09

Sergey Berezovskiy