0

I am setting up a function in a unit test and want to return result of same function that is being setup.

Interface looks like this.

 public interface ICommonServices
{
    int[] GetIntArray();
    int AddOne(int a);
}

Interface is implemented like this.

  public int AddOne(int a)
    {
        return a + 1;
    }
    public int[] GetIntArray()
    {
        int[] x = { 1, 2, 3, 4 };
        return x;
    }

Service Under Test (ObservationService) has a function that needs to be tested.

public string SomeCalculation()
    {
        var intArray = _commonServices.GetIntArray();
        List<int> result = intArray.Select(x => _commonServices.AddOne(x)).ToList();
        //Do Something With result.
        return "Done";
    }

where _commonServices is injecting using a DI.

and my Unit Test Looks like this

   [Test]
    public void Some_Test()
    {
        int[] a = { 5, 6, 7, 8 };
        using (var mock = AutoMock.GetLoose())
        {
            // Arrange          
            mock.Mock<ICommonServices>().Setup(x => x.GetIntArray()).Returns(a);
            mock.Mock<ICommonServices>().Setup(x => x.AddOne(It.IsAny<int>())).Returns(/* what to do here AddOne(x)   */);
            var sut = mock.Create<ObservationService>();
            // Act
            var res = sut.SomeCalculation();
            // Assert
            Assert.AreEqual("Done", res);
        }
    }

I am trying to Setup "AddOne" function to return the result of its own.

Raas Masood
  • 1,123
  • 14
  • 46
  • Why to mock something which does only `a + 1`? It is not necessary to mock it at all. – Daniel Dušek Sep 08 '16 at 17:55
  • this is just an example. in real code lot more is happening. – Raas Masood Sep 08 '16 at 17:57
  • So then you will probably have to simplify the test so it tests some more specific case and to return exactly the value of the function AddOne which corresponds with this specific test case? – Daniel Dušek Sep 08 '16 at 18:01
  • What are the conditions for the test? Could you use this naming pattern? [UnitOfWork_StateUnderTest_ExpectedBehavior](http://stackoverflow.com/questions/155436/unit-test-naming-best-practices). – Daniel Dušek Sep 08 '16 at 18:05
  • your sut should not be mocked. you mock the dependencies of the sut – Nkosi Sep 08 '16 at 18:12

2 Answers2

1

I hope I understood you correctly.

Here is what you could do:

mock.Mock<ICommonServices>().Setup(x => x.AddOne(It.IsAny<int>())).Returns( (int a) =>
                                                                        {
                                                                            return a + 1;
                                                                        });

How to reuse the AddOne() method:

  1. Declare your AddOne() as static
public class CommonServices : ICommonServices
{
    public static int AddOne(int a)
    {
        return a + 1;
    }
}
  1. Now you can use your static method in your mockup
mock.Mock<ICommonServices>().Setup(x => x.AddOne(It.IsAny<int>())).Returns( (int a) =>
                                                                            {
                                                                                return CommonServices.AddOne(a);
                                                                            });
Konstantin
  • 391
  • 1
  • 3
  • 13
  • but than i have to write a+ 1 function again. cant i use the same function? – Raas Masood Sep 08 '16 at 17:52
  • Sure you can, but then you need to declare you `AddOne` method as static. In this case please refer to this post: [Mock a Method with Moq and make moq.Object available there](http://stackoverflow.com/questions/37147215/mock-a-method-with-moq-and-make-moq-object-available-there) – Konstantin Sep 08 '16 at 17:53
  • but than i am changing my code to suit my test which is against the unit testing concepts. isn't it ? – Raas Masood Sep 08 '16 at 17:54
0

solution was to moq the CommonService

   var cs = mock.Create<CommonServices>();

and use it in return

mock.Mock<ICommonServices>().Setup(x => x.AddOne(It.IsAny<int>())).Returns( (int a) =>
                                                                        {
                                                                            return cs.AddOne(a);
                                                                        });
Raas Masood
  • 1,123
  • 14
  • 46