title: How to mock out IEndpointInstance of NServiceBus
body: <p>I have the controller below which communicates via NServiceBus IEndpointInstance (full duplex response/request solution). I would like to test validation placed in this controller, so I need to pass through an IEndpointInstance object. Unfortunately, there is no mention about this in the documentation in Particular's site that I could find.</p>
  
  <p>In the <a href="https://www.nuget.org/packages/NServiceBus.Testing/6.0.3" rel="nofollow noreferrer">NServiceBus.Testing</a> nuget package I found the TestableEndpointInstance class, but I don't know how to use it.</p>
  
  <p>I have the test code below and it compiles, but it just hangs when I run it. I think something is wrong around TestableEndpointInstance parametrization.</p>
  
  <p>Could someone help me out with an example?</p>
  
  <p><strong>Controller</strong>:</p>
  
  <pre><code>public CountryController(
      IEndpointInstance endpointInstance,
      IMasterDataContractsValidator masterDataContractsValidator)
  {
      this.endpointInstance = endpointInstance;
      this._masterDataContractsValidator = masterDataContractsValidator;
  }
  
  [HttpPost]
  [Route("Add")]
  public async Task&lt;HttpResponseMessage&gt; Add([FromBody] CountryContract countryContract)
  {
      try
      {
          CountryRequest countryRequest = new CountryRequest();
          this._masterDataContractsValidator.CountryContractValidator.ValidateWithoutIdAndThrow(countryContract);
  
          countryRequest.Operation = CountryOperations.Add;
          countryRequest.CountryContracts.Add(countryContract);
  
          // nservicebus communication towards endpoint
  
          return message;
      }
      catch (Exception e)
      {
          var message = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
          return message;
      }
  }
  </code></pre>
  
  <p><strong>Test</strong>:</p>
  
  <pre><code>public CountryControllerTests()
  {
      TestableEndpointInstance endpointInstance = new TestableEndpointInstance();
      // Validator instantiation
      this.countryController = new CountryController(endpointInstance, masterDataContractsValidator);
  }
  
  
  [Theory]
  [MemberData("CountryControllerTestsAddValidation")]
  public async void CountryControllerTests_Add_Validation(
      int testId,
      CountryContract countryContract)
  {
      // Given
  
      // When
      Func&lt;Task&gt; action = async () =&gt; await this.countryController.Add(countryContract);
  
      // Then
      action.ShouldThrow&lt;Exception&gt;();
  }
  </code></pre>