Skip to content

Commit

Permalink
fix: 🐛 fix errors in the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Aug 21, 2024
1 parent 3456462 commit e8aab56
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 57 deletions.
4 changes: 0 additions & 4 deletions .env

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,5 @@ coverage.info

# Built Visual Studio Code Extensions
*.vsix

.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoBogus;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using NSubstitute;
Expand Down Expand Up @@ -32,40 +33,34 @@ public async Task get_all_users_should_call_http_client_with_valid_parameters_on

var options = Substitute.For<IOptions<UsersHttpClientOptions>>();
var usersHttpClientOptions = new UsersHttpClientOptions
{
UsersEndpoint = "users",
BaseAddress = "http://example.com"
};
{
UsersEndpoint = "users", BaseAddress = "http://example.com"
};
options.Value.Returns(usersHttpClientOptions);
var url =
$"{
usersHttpClientOptions.BaseAddress
}/{
usersHttpClientOptions.UsersEndpoint
}*";
$"{usersHttpClientOptions.BaseAddress}/{usersHttpClientOptions.UsersEndpoint}*";

var usersClient = new AutoFaker<UserClientDto>().Generate(total);

var usersListPage = new UsersListPageClientDto
{
Total = total,
Limit = pageSize,
Skip = page,
Users = usersClient
};
{
Total = total, Limit = pageSize, Skip = page, Users = usersClient
};

// https://github.com/richardszalay/mockhttp
// https://code-maze.com/csharp-mock-httpclient-with-unit-tests/
var mockHttp = new MockHttpMessageHandler();

// Setup a respond for the user api (including a wildcard in the URL)
var request = mockHttp.When(url).Respond("application/json", JsonConvert.SerializeObject(usersListPage)); // Respond with JSON
var request =
mockHttp.When(url)
.Respond("application/json", JsonConvert.SerializeObject(usersListPage)); // Respond with JSON

// Inject the handler or client into your application code
var client = mockHttp.ToHttpClient();
client.BaseAddress = new Uri(usersHttpClientOptions.BaseAddress);

var pageRequest = new PageRequest { PageNumber = page, PageSize = pageSize };
var pageRequest = new PageRequest {PageNumber = page, PageSize = pageSize};

var usersHttpClient = new UsersHttpClient(client, _mappingFixture.Mapper, options);

Expand All @@ -86,40 +81,34 @@ public async Task get_all_users_should_return_users_list()

var options = Substitute.For<IOptions<UsersHttpClientOptions>>();
var usersHttpClientOptions = new UsersHttpClientOptions
{
UsersEndpoint = "users",
BaseAddress = "http://example.com"
};
{
UsersEndpoint = "users", BaseAddress = "http://example.com"
};
options.Value.Returns(usersHttpClientOptions);
var url =
$"{
usersHttpClientOptions.BaseAddress
}/{
usersHttpClientOptions.UsersEndpoint
}*";
$"{usersHttpClientOptions.BaseAddress}/{usersHttpClientOptions.UsersEndpoint}*";

var usersClient = new AutoFaker<UserClientDto>().Generate(total);

var usersListPage = new UsersListPageClientDto
{
Total = total,
Limit = pageSize,
Skip = page,
Users = usersClient
};
{
Total = total, Limit = pageSize, Skip = page, Users = usersClient
};

// https://github.com/richardszalay/mockhttp
// https://code-maze.com/csharp-mock-httpclient-with-unit-tests/
var mockHttp = new MockHttpMessageHandler();

// Setup a respond for the user api (including a wildcard in the URL)
var request = mockHttp.When(url).Respond("application/json", JsonConvert.SerializeObject(usersListPage)); // Respond with JSON
var request =
mockHttp.When(url)
.Respond("application/json", JsonConvert.SerializeObject(usersListPage)); // Respond with JSON

// Inject the handler or client into your application code
var client = mockHttp.ToHttpClient();
client.BaseAddress = new Uri(usersHttpClientOptions.BaseAddress);

var pageRequest = new PageRequest { PageNumber = page, PageSize = pageSize };
var pageRequest = new PageRequest {PageNumber = page, PageSize = pageSize};

var users = _mappingFixture.Mapper.Map<IList<User>>(usersClient);

Expand All @@ -140,25 +129,25 @@ public async Task get_all_users_with_http_response_exception_should_throw_http_r
// Arrange
var pageSize = 10;
var page = 1;
var total = 20;

var options = Substitute.For<IOptions<UsersHttpClientOptions>>();
var usersHttpClientOptions = new UsersHttpClientOptions
{
UsersEndpoint = "users",
BaseAddress = "http://example.com"
};
{
UsersEndpoint = "users", BaseAddress = "http://example.com"
};
options.Value.Returns(usersHttpClientOptions);

// https://github.com/richardszalay/mockhttp
// https://code-maze.com/csharp-mock-httpclient-with-unit-tests/
var mockHttp = new MockHttpMessageHandler();
mockHttp.Fallback.Throw(
new HttpResponseException(StatusCodes.Status500InternalServerError, "There is an error in the server"));

// Inject the handler or client into your application code
var client = mockHttp.ToHttpClient();
client.BaseAddress = new Uri(usersHttpClientOptions.BaseAddress);

var pageRequest = new PageRequest { PageNumber = page, PageSize = pageSize };
var pageRequest = new PageRequest {PageNumber = page, PageSize = pageSize};

var usersHttpClient = new UsersHttpClient(client, _mappingFixture.Mapper, options);

Expand All @@ -175,34 +164,29 @@ public async Task get_all_users_with_exception_should_throw_exception()
// Arrange
var pageSize = 10;
var page = 1;
var total = 20;

var options = Substitute.For<IOptions<UsersHttpClientOptions>>();
var usersHttpClientOptions = new UsersHttpClientOptions
{
UsersEndpoint = "users",
BaseAddress = "http://example.com"
};
{
UsersEndpoint = "users", BaseAddress = "http://example.com"
};
options.Value.Returns(usersHttpClientOptions);
var url =
$"{
usersHttpClientOptions.BaseAddress
}/{
usersHttpClientOptions.UsersEndpoint
}*";
$"{usersHttpClientOptions.BaseAddress}/{usersHttpClientOptions.UsersEndpoint}*";

// https://github.com/richardszalay/mockhttp
// https://code-maze.com/csharp-mock-httpclient-with-unit-tests/
var mockHttp = new MockHttpMessageHandler();

// Setup a respond for the user api (including a wildcard in the URL)
var request = mockHttp.When(url).Respond("application/json", JsonConvert.SerializeObject(null)); // Respond with JSON
// Setup a response for the user api (including a wildcard in the URL)
var request =
mockHttp.When(url).Respond("application/json", JsonConvert.SerializeObject(null)); // Respond with JSON

// Inject the handler or client into your application code
var client = mockHttp.ToHttpClient();
client.BaseAddress = new Uri(usersHttpClientOptions.BaseAddress);

var pageRequest = new PageRequest { PageNumber = page, PageSize = pageSize };
var pageRequest = new PageRequest {PageNumber = page, PageSize = pageSize};

var usersHttpClient = new UsersHttpClient(client, _mappingFixture.Mapper, options);

Expand Down

0 comments on commit e8aab56

Please sign in to comment.