Skip to content

Instantly share code, notes, and snippets.

@mrstebo
Last active April 19, 2021 14:52
Show Gist options
  • Save mrstebo/3636d3f86a4fe8e27205f2c4a0065f27 to your computer and use it in GitHub Desktop.
Save mrstebo/3636d3f86a4fe8e27205f2c4a0065f27 to your computer and use it in GitHub Desktop.
Example for mocking RestSharps IRestClient ExecuteAsync method using Moq
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);
});
}
}
}
@morrisond91
Copy link

Hey do you have any examples in NSubstitute?

@mrstebo
Copy link
Author

mrstebo commented Jan 27, 2020

Sorry @morrisond91, unfortunately not 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment