Last active
April 19, 2021 14:52
-
-
Save mrstebo/3636d3f86a4fe8e27205f2c4a0065f27 to your computer and use it in GitHub Desktop.
Example for mocking RestSharps IRestClient ExecuteAsync method using Moq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Moq; | |
using NUnit.Framework; | |
using RestSharp; | |
using System; | |
using System.Net; | |
namespace RestsharpTests | |
{ | |
[TestFixture] | |
public class RestsharpExecuteAsyncMoq | |
{ | |
[Test] | |
public void ExecuteAsyncWithMoq() | |
{ | |
var restClient = new Mock<IRestClient>(); | |
restClient | |
.Setup(x => x.ExecuteAsync( | |
It.IsAny<IRestRequest>(), | |
It.IsAny<Action<IRestResponse, RestRequestAsyncHandle>>())) | |
.Callback<IRestRequest, Action<IRestResponse, RestRequestAsyncHandle>>((request, callback) => | |
{ | |
callback(new RestResponse { StatusCode = HttpStatusCode.OK }, null); | |
}); | |
restClient.Object.ExecuteAsync(new RestRequest(), (response, handle) => | |
{ | |
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey do you have any examples in NSubstitute?