NSubstitute.Exceptions.ReceivedCallsException: Expected to receive a call matching error while unit testing

0

I am getting a below error while unit testing the piece of code

NSubstitute.Exceptions.ReceivedCallsException: Expected to receive a call matching error while unit testing NSubstitute.Exceptions.ReceivedCallsException HResult=0x80131500 Message=Expected to receive a call matching: enrollWithHelper("", "") Actually received no matching calls.

Source=NSubstitute StackTrace: at NSubstitute.Core.ReceivedCallsExceptionThrower.Throw(ICallSpecification callSpecification, IEnumerable1 matchingCalls, IEnumerable1 nonMatchingCalls, Quantity requiredQuantity) at NSubstitute.Routing.Handlers.CheckReceivedCallsHandler.Handle(ICall call) at NSubstitute.Routing.Route.Handle(ICall call) at NSubstitute.Proxies.CastleDynamicProxy.CastleForwardingInterceptor.Intercept(IInvocation invocation) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.ObjectProxy_1.enrollWithHelper(String name, String type) at MyProject.MyTest.processing_test_pass() in C:\MyProject\MyTest.cs:line 75

Framework: .NET Core

Unit Testing: NSubstitute

Here is my class under test

 public class MyService: IMysService
 {
   private readonly IRepository _repository;
   Private readonly IIoTHelper _iotHelper;  
   Private readonly HttpClient _httpClient;
   private Configuration _configuration; 

   public MyService(IRepository repository, IIoTHelper iotHelper, HttpClient httpClient )
   {
        _repository = repository;
        _iotHelper =iotHelper;
        _httpClient = httpClient ;
   }

   public bool CallService(JObject reqObj, out Status status)
   {
        bool provisioningSuccess = false;
        return PreProcessing(reqObj,out Status status); //private method
   }
}

Here is my private method

    private PreProcessing(JObject JosnObj, out Status status)
    {
        if (_configuration== null)
        {
            _configuration= _repository.GetConfigurations()                 
        }

       using (var client = this._httpClient)
       {
            var request = new {_configuration.Number,_configuration.Type};
            var response = client.PostAsJsonAsync("api/preprocess", request).Result;

            if (response.IsSuccessStatusCode)
            {
                  _iotHelper.enrollWithHelper(_configuration.Number,_configuration.Type);
            }
       }
    }

Here is my Configuration class

   public class Configuration 
   {
     public Configuration (string number, string type)
     {
        Number= number;
        Type= type;
     }      
     public string Number { get;}

     public string Type { get; }

   }

Here is my unit test code, where I want to make sure it reaches the private method

    protected override void InitializeTest()
    {
        this._repository = Substitute.For<IRepository>();
        this._iotHelper = Substitute.For<IIotHelper>();
    }

    [TestMethod]
    public void processing_test_pass()
    {
          //Arrange
        var messageHandler = new MockHttpMessageHandler("TEST VALUE", HttpStatusCode.OK);
        var httpClient = new HttpClient(messageHandler);


          JObject reqobj= new JObject(
          new JProperty("user", "username"),
          new JProperty("pass", "password"));

        Status status = new Status();

        //act
        var t = new MyService(this._repository,this._iotHelper, httpClient );
        bool success = t.CallService(reqobj, out status);
        //assert
        this._iotHelper.Received().enrollWithHelper("","");

    }

How would I test enrollWithHelper is called?

Also I am stuck due to error!

c#
unit-testing
nsubstitute
asked on Stack Overflow Jan 16, 2020 by kudlatiger • edited Jan 17, 2020 by kudlatiger

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0