title: Azure Functions testing when using imperative binding
body: <p>So far I've been able to setup unit testing for Azure Functions and it works great. However for my current project I need to use dynamic or imperative bindings.
  <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#imperative-bindings" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#imperative-bindings</a></p>
  
  <p>This leads to issues for my unit test I cannot seem to solve.</p>
  
  <p>My function looks like this:</p>
  
  <pre><code>using Microsoft.Azure.WebJobs;
  using Microsoft.Azure.WebJobs.Extensions.Http;
  using Microsoft.ServiceBus.Messaging;
  using Newtonsoft.Json;
  using System;
  using System.IO;
  using System.Net;
  using System.Net.Http;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace My.Functions
  {
      public static class MyFunc
      {
          [FunctionName("my-func")]
          public static async Task&lt;HttpResponseMessage&gt; Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req,
              Binder binder)
          {
              dynamic data = await req.Content.ReadAsAsync&lt;object&gt;();
              byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
              MemoryStream stream = new MemoryStream(bytes, writable: false);
  
              var sbMsg = new BrokeredMessage(stream) { ContentType = "application/json" };
              var attributes = new Attribute[]
              {
                  new ServiceBusAccountAttribute("some-sb-account"),
                  new ServiceBusAttribute("some-queue-or-topic", AccessRights.Send)
              };
              var outputSbMessage = await binder.BindAsync&lt;IAsyncCollector&lt;BrokeredMessage&gt;&gt;(attributes);
              await outputSbMessage.AddAsync(sbMsg);
  
              return req.CreateResponse(HttpStatusCode.OK, "OK");
          }
      }
  }
  </code></pre>
  
  <p>Near the end of the code of the function, I configure this binder to hold a list of BrokeredMessages. This is done by calling the BindAsync on the binder.</p>
  
  <p>The attributes are dynamically set and contain a servicebus connection and topic name. This all works great when deployed to Azure so functionality-wise everything is fine. 
  So far so good.</p>
  
  <p>However I'm stuggling with getting my test running. To be able to invoke the function, I need to provide parameters. The HttpTrigger this is pretty common, but for the Binder I don't know what to provide.</p>
  
  <p>For testing I use this approach:</p>
  
  <pre><code>[TestMethod]
  public void SendHttpReq()
  {
      // Setup
      var httpRequest = GetHttpRequestFromTestFile("HttpRequest");
      var sbOutput = new CustomBinder();
  
      // Act
      var response = SendToServicebus.Run(httpRequest, sbOutput);
  
      // Assert
      Assert.AreEqual(sbOutput.Count(), 1);
  
      // Clean up
  }
  </code></pre>
  
  <p>I use a CustomBinder inherited from Binder, because just having an instance of Binder failed in the function on the 'BindAsync' throwing 'Object reference not set to an instance of an object'. It seems the constructor of the binder is actually not meant to be called.</p>
  
  <p>In the CustomBinder I override the BindAsync to return a generic list of BrokeredMessages.</p>
  
  <pre><code>public class CustomBinder : Binder
  {
   public override async Task&lt;TValue&gt; BindAsync&lt;TValue&gt;(Attribute[] attributes, CancellationToken cancellationToken = new CancellationToken())
   {
    return (TValue)((object)(new List&lt;BrokeredMessage&gt;()));
    }
  }
  </code></pre>
  
  <p>Not entirely surprising that also failed throwing:</p>
  
  <blockquote>
    <p>InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List'1[Microsoft.ServiceBus.Messaging.BrokeredMessage]' to type 'Microsoft.Azure.WebJobs.IAsyncCollector`1[Microsoft.ServiceBus.Messaging.BrokeredMessage]'.</p>
  </blockquote>
  
  <p>I cannot find an implementation of the IAsyncCollector, so maybe I need to approach this differently? </p>
  
  <p>My actual goal is to be able to verify the list of brokered messages, as the function would output to Azure servicebus.</p>