GithubHelp home page GithubHelp logo

zinknotthemetal / moq.restsharp.helpers Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 2.0 56 KB

Helpers used when unit testing with Moq and RestSharp

License: GNU General Public License v3.0

C# 100.00%

moq.restsharp.helpers's Issues

Proxy issue

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 :)

How to mock different requests / responses with similar types?

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?

Mocked ExecutePostAsync always returns null

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.

Updater service class

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}");
		}
	}

}

Test factory

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 case

[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));
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.