6

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?

Community
  • 1
  • 1
Morgan Soren
  • 563
  • 4
  • 14
  • 33

1 Answers1

12

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
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • I'm missing something here. I don't have a controller (it's web forms). I'll put further information in the question. – Morgan Soren Dec 10 '12 at 17:18
  • @MorganSoren controller was just an example. It could be any other class which uses your service adapter. I think web forms is not very unit testable as is, usually MVP pattern used to create testable web forms applications. – Sergey Berezovskiy Dec 10 '12 at 17:26
  • @MorganSoren here is question about [unit-testing web forms](http://programmers.stackexchange.com/questions/116089/is-is-possible-to-unit-test-a-web-forms-site) – Sergey Berezovskiy Dec 10 '12 at 17:28
  • I didn't have a class that used IServiceAdapter. I created one, but I'm not sure what to put in there. I did something like this: public FakeController(IServiceAdapter Service) and I referenced it like: this.Service = Service; However that will return the same string as always. Should I put something like Service = new ServiceAdapter(); before calling the Save method? If I do that, it will try to save in the database =/. – Morgan Soren Dec 10 '12 at 18:41
  • @MorganSoren usually [dependency injection](http://stackoverflow.com/questions/589374/how-to-use-dependency-injection-with-asp-net) used for providing abstract dependencies implementations. – Sergey Berezovskiy Dec 10 '12 at 18:45
  • Here's a question, how would you test implementation of IServiceAdapter? Would it be with Moq? – activebiz Jan 31 '13 at 12:47
  • 1
    @Preyash if implementation of IServiceAdapter have dependencies, which should be mocked, then yes, I'd used some mocking framework for testing – Sergey Berezovskiy Jan 31 '13 at 12:54
  • @lazyberezovsky i .c. so if IServiceAdapter does not have dependency then there is no point of mocking that? you just test the concrete implementation? – activebiz Jan 31 '13 at 13:00
  • 1
    @Preyash exactly :) No dependencies - no mocks. You should not use real dependencies, classes should be [tested in isolation](http://agileinaflash.blogspot.com/2012/04/is-your-unit-test-isolated.html) – Sergey Berezovskiy Jan 31 '13 at 13:09