lundi 21 août 2017

Mock returns null value when ReturnResult is a custom object but works as expected when it is a primitive type bool

I am using Moq in .net core(1.1) and having a bit of torrid time understanding this behaviour as all the examples on interweb points to the fact the this should work with no issues.

I have already tried with:

  • Returns(Task.FromResult(...)
  • Returns(Task.FromResult(...)
  • ReturnsAsync(...)

    var mockClient = new Mock();

Does not work:

mockClient.Setup(x => x.PostAsync(url, JsonConvert.SerializeObject(body), null)).Returns(Task.FromResult(new ApiResponse()));

PostSync definition:

public async Task<ApiResponse> PostAsync(string url, string body, string authToken = null)

Does work:

mockClient.Setup(x => x.PostAsync(url, JsonConvert.SerializeObject(body), null)).Returns(Task.FromResult(bool));

PostSync definition:

public async Task<bool> PostAsync(string url, string body, string authToken = null)

Usage:

var api = new ApiService(mockClient.Object);
var response = api.LoginAsync(body.Username, body.Password);

Happy to elaborate more if needed. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire