title: Mocking SDK client which has chained methods
body: <p>I need to unit test <em>Test1()</em> as below and have to mock <a href="https://github.com/microsoftgraph/msgraph-sdk-dotnet" rel="nofollow noreferrer">msgraph-sdk</a> create event which has http call.<br>
  I was mocking <em>Request()</em> and <em>AddAsync( )</em> methods but got null value on Act.<br>
  What is the correct way to unit test <em>CreateAsync</em> by mocking ?</p>
  
  <pre><code>public interface IApiClient
  {
      Task&lt;Event&gt; CreateAsync(EventItem eventItem);
  }
  
  public class ApiClient : IApiClient
  {
      private readonly IGraphServiceClient _graphServiceClient;
  
  
      public ApiClient(IGraphServiceClient graphServiceClient)
      {
          _graphServiceClient = graphServiceClient;
      }
  
      public async Task&lt;Event&gt; CreateAsync(EventItem eventItem)
      {
          var item = new Event
          {
              Subject = eventItem.Subject
          };
          return await _graphServiceClient.Users[eventItem.UserInfo.Id].Events
              .Request()
              .AddAsync(item);
      }
  
  }
  
  public class ApiClientTest
  {
      private Mock&lt;IGraphServiceClient&gt; _graphServiceClientMock;
      private IApiClient _apiClient;
      private Mock&lt;IUserEventsCollectionRequest&gt; _mockUserEventsCollectionRequest;
      private EventItem _eventItem;
  
      [SetUp]
      public async System.Threading.Tasks.Task SetupAsync()
      {
          var x = new Microsoft.Graph.Event()
          {
              Subject = "123"
          };
  
          _graphServiceClientMock = new Mock&lt;IGraphServiceClient&gt;();
          _mockUserEventsCollectionRequest = new Mock&lt;IUserEventsCollectionRequest&gt;();
          _apiClient = new IApiClient(_graphServiceClientMock.Object);
  
          _graphServiceClientMock.Setup(g =&gt; g.Users[_eventItem.UserInfo.Id].Events
                                 .Request())
                                 .Returns(
          _mockUserEventsCollectionRequest.Setup(g =&gt; g.AddAsync(x))
                                 .ReturnsAsync(new Event { Id = "asaasaS" });
  
      }
  
      [Test]
      public async Task Test1()
      {
          // Act
          var newEvent = await _apiClient.CreateAsync(_eventItem);
          // Assert
          Assert.NotNull(newEvent);
          Assert.AreEqual("asaasaS", newEvent.Id);
      }
  }   
  </code></pre>
  
  <p>I was mocking <em>Request()</em> and <em>AddAsync( )</em> methods but got null value on Act.<br>
  What is the correct way to unit test <em>CreateAsync</em> by mocking ?</p>