Skip to content

cioina/MyTested-test-project-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MyTested-test-project-example

Introduction

The compiled code of our .NET Core 8 application is on our GitHub repository. For this test project, which is part our application, we will use MyTested - a well-known library for testing ASP.NET Core MVC. Here, we adapted the library to work with .NET Core 8 and API controllers with Bearer Header Authorization based on JWT token implementation provided by .NET Core. Our .NET Core 8 project is based on BookStore repository and adapted to work with MyTested library. In addition, we used NSwag.MSBuild from CleanArchitecture to generate specification.json.

MyTested Library Out of The Box

I found out about MyTested for the first time from BlazorShop and CarRentalSystem repositories. At the same time, I found out about Jwt Authentication implementation from BlazorShop and RealWorld repositories. Both Jwt Authentication implementations did not work with original MyTested library, so I decided to find out why. I do not know who engineered MyTested, but I was not able to fully understand how it works. I was able only to add some small pieces of code to make MyTested and my own Jwt Authentication implementation work and not to break any original MyTested tests. But, what MyTested can do out of the box? The best answer is in MusicStore testing project. For the API controller, here is an example:

namespace BlogAngular.Test.Routing;

using Application.Common.Version;
using MyTested.AspNetCore.Mvc;
using Web.Features;
using Xunit;

public class FrontEndRouteTest
{
    [Fact]
    public void VersionShouldBeRouted()
    => MyMvc
        .Pipeline()
        .ShouldMap(request => request
            .WithMethod(HttpMethod.Get)
            .WithLocation("api/v1.0/version"))
        .To<VersionController>(c => c.Index())
        .Which()
        .ShouldReturn()
        .ActionResult(result => result.Result(new VersionResponseEnvelope
        {
            VersionJson = new VersionResponseModel()
        }));
}

Basic API Controller Testing

By basic API controller testing, we mean at least one test per CRUD concept. Here is an example:

  • Create_tag_should_return_success_with_data- Create
  • Listing_tags_without_url_parameters_should_return_success_with_all_tags- Read
  • Edit_tag_should_return_success_with_data- Update
  • Delete_tag_should_return_success_with_tag_id - Delete

A particular change we made to MyTested is adding the possibility of testing data validation. In fact, now, we can realize all following tests: BookStore, RealWorld, CleanArchitecture1, and CleanArchitecture2 in a set of beautiful tests. Here are examples of testing data validation using modified version of MyTested library:

  • Create_tag_with_one_char_should_return_validation_error- Creates tag name length bellow allowed by database constraint
  • Create_tag_with_max_chars_should_return_validation_error- Creates tag name length above allowed by database constraint
  • Edit_tag_with_one_char_should_return_validation_error- Updates tag name length bellow allowed by database constraint
  • Edit_tag_with_max_chars_should_return_validation_error - Updates tag name length above allowed by database constraint

Our validation implementation is based mostly on BookStore. One useful technique to validate unique data comes from Conduit and CleanArchitecture

  • Create_tag_with_same_name_should_fail_with_validation_error- Creates tag name length bellow allowed by database constraint
  • Edit_tag_with_same_name_should_fail_with_validation_error- Creates tag name length above allowed by database constraint
  • Edit_same_tag_with_same_name_should_return_success_with_data- Updates tag name length bellow allowed by database constraint

In our application, any MyTested.AspNetCore.Mvc.Exceptions.ValidationErrorsAssertionException will return 422 with JSON string similar to this:

{
   "TagJson.Title":  ["The length of 'Tag Json Title' must be 420 characters or fewer. You entered 421 characters."]
}

That represents a standard validation message from FluentValidation library which can be customized.

MyTested Library Limitations

We applied modified version of MyTested library to three popular GitHub repositories: BookStore, RealWorld, and CleanArchitecture. Our quick investigation shows that BookStore can be configurated to work 100% with MyTested while RealWorld works only with anonymous controllers and CleanArchitecture does not work at all.

Releases

No releases published

Packages

No packages published

Languages