jeudi 8 septembre 2016

setup function to return result of same function that is being setup

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.

Aucun commentaire:

Enregistrer un commentaire