zinknotthemetal / moq.restsharp.helpers Goto Github PK
View Code? Open in Web Editor NEWHelpers used when unit testing with Moq and RestSharp
License: GNU General Public License v3.0
Helpers used when unit testing with Moq and RestSharp
License: GNU General Public License v3.0
I tried to run my code and I'm getting:
System.TypeLoadException : Type 'Castle.Proxies.IRestResponse`1Proxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' is attempting to implement an inaccessible interface.
Can't find a sensible way to add or change the interface. I'll appreciate any help :)
Hi William
Thank you for sharing your library / code.
I hope this project helps you out with your unit testing endevours. If you need any help feel free to contact me.
I have a question re: your library:
public interface IPeopleRepository
{
Task<List<Person>> GetPeopleFromRepository();
}
public class FakeApi : IPeopleRepository
{
private readonly IRestClient _restClient;
public FakeApi(IRestClient restClient)
{
_restClient = restClient;
}
public void MakeManyRequests()
{
_restClient.Execute<TypeA>()(request1);
_restClient.Execute<TypeA>()(request2);
_restClient.Execute<TypeB>()(request3); // TypeB
}
}
This is essentially what is happening in my library (above):
I will be essentially making two responses that are of the same type (notice request1 and request2):
var response = MockRestClient
.MockApiRequest<List<TypeA>>()
.WithStatusCode(HttpStatusCode.InternalServerError)
.WithResponseStatus(ResponseStatus.Error)
.WithErrorMessage("API seems to be down at the moment, please try again later")
.MockExecute();
I was wondering if there was some way I could mock both requests so that they return different values?
Hi,
i have an issue with mocking the ExecutePostAsync
method of the RestSharp client. It seems no matter what i do, the mocked method ExecutePostAsync
always returns null
. I'm not sure if i'm doing anything wrong.
I have the following setup. I am using .NET 5.0 as my Visual Studio 2019 does not support any higher framework. To make it possible to use your library, i am using RestSharp 106.15.0, which is not the latest version but it's compatible with your lib, which's version is 2021.4.30.
public class UpdaterService : IUpdateService
{
private readonly IRestClient _restClient;
public UpdaterService(IRestClient restClient)
{
_restClient = restClient ?? throw new ArgumentNullException(nameof(restClient));
}
public async Task UpdateActuators(IEnumerable<Actuator> item)
{
await UpdateObject<Actuator>(Constants.RestApiPaths.Actuators, item);
}
private async Task UpdateObject<TObject>(string path, IEnumerable<TObject> items) where TObject : BaseItem
{
if (items.IsNullOrEmpty())
{
throw new ArgumentNullException(nameof(items));
}
var request = new RestRequest(path, Method.POST);
request.AddBody(items);
var response = await _restClient.ExecutePostAsync(request)
.ConfigureAwait(false);
if (!IsValidStatusCode(response))
{
throw new InvalidResponseException($"StatusCode={response?.StatusCode}");
}
}
}
public static Mock<IRestClient> GetRestClientMockUpdateAsync<TItem>() where TItem : BaseItem
{
var restClientMock = new Mock<IRestClient>();
var response = new RootObject<TItem>();
restClientMock
.MockApiResponse<RootObject<TItem>>()
.WithStatusCode(System.Net.HttpStatusCode.OK)
.WithResponseStatus(ResponseStatus.Completed)
.Returns(response)
.MockExecutePostAsync();
return restClientMock;
}
[Test]
public void UpdateActuators_WithValidInput_DoesNotThrow()
{
// Arrange
var restClientMock = TestData.GetRestClientMockUpdateAsync<Actuator>();
var service = new UpdaterService(restClientMock.Object);
// Act
Assert.DoesNotThrowAsync(() => service.UpdateActuators(TestModels.RandomActuators));
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.