SkillAgentSearch skills...

MyTested.WebApi

Fluent testing framework for ASP.NET Web API 2.

Install / Use

/learn @ivaylokenov/MyTested.WebApi

README

<h1><img src="https://raw.githubusercontent.com/ivaylokenov/MyTested.WebApi/master/documentation/logo.png" align="left" alt="MyTested.WebApi" width="100">&nbsp; MyTested.WebApi - Fluent testing<br />&nbsp; framework for ASP.NET Web API 2</h1>

Diamond Sponsors

<table> <tbody> <tr> <td align="center" valign="middle"> <a href="http://bit.ly/3da6h7f" target="_blank"> <img width="323px" src="https://user-images.githubusercontent.com/3391906/77253785-5b734880-6c65-11ea-92d5-71dcfc833fe0.png"> </a> </td> </tr> </tbody> </table>

Gold Sponsors

<table> <tbody> <tr> <td align="center" valign="middle"> <a href="https://bit.ly/ciu-zuehlke" target="_blank"> <img width="148px" src="https://user-images.githubusercontent.com/3391906/84595391-4ec75080-ae60-11ea-8bc4-2b5e4b17a345.jpg"> </a> </td> <td align="center" valign="middle"> <a href="https://softuni.org/" target="_blank"> <img width="148px" src="https://softuni.org/platform/assets/icons/logo.svg"> </a> </td> <td align="center" valign="middle"> <a href="http://bit.ly/30xsnsC" target="_blank"> <img width="148px" src="https://user-images.githubusercontent.com/3391906/65251792-dd848800-daef-11e9-8857-637a48048cda.png"> </a> </td> <td align="center" valign="middle"> <a href="http://noblehire.io?utm_medium=social&utm_source=projects&utm_campaign=platform-leads-knv" target="_blank"> <img width="148px" src="https://user-images.githubusercontent.com/3391906/66921689-637fea00-f02e-11e9-944a-b07c6f345a06.png"> </a> </td> <td align="center" valign="middle"> <a href="http://bit.ly/onebitsoftware" target="_blank"> <img width="148px" height="70px" src="https://user-images.githubusercontent.com/3391906/69410626-1a4d4500-0d14-11ea-905f-c1705b6364bf.png"> </a> </td> </tr> </tbody> </table>

Special Sponsors

<table> <tbody> <tr> <td align="center" valign="middle"> <a href="http://bit.ly/bellatrixsolutions" target="_blank"> <img width="148px" src="https://user-images.githubusercontent.com/3391906/68993273-d4f5c700-087e-11ea-9b39-e173733fcbfb.png" alt="The Ultimate Cross-Platform .NET Framework"> </a> </td> <td align="center" valign="middle"> <a href="https://www.jetbrains.com/?from=MyTestedASP.NET" target="_blank"> <img width="148px" src="https://user-images.githubusercontent.com/3391906/72542498-ee21f080-388c-11ea-92ac-0b0153028933.png" alt="JetBrains"> </a> </td> </tr> </tbody> </table>

Project Description

MyTested.WebApi is a unit testing library providing easy fluent interface to test the ASP.NET Web API 2 framework. It is testing framework agnostic, so you can combine it with a test runner of your choice (e.g. NUnit, xUnit, etc.). Inspired by ChaiJS.

Build status NuGet Version Coverage Status License

Sponsors & Backers

MyTested.WebApi is a community-driven open source library. It's an independent project with its ongoing development made possible thanks to the support by these awesome backers. If you'd like to join them, please consider:

What's the difference between Patreon and OpenCollective?

Funds donated via both platforms are used for development and marketing purposes. Funds donated via OpenCollective are managed with transparent expenses. Your name/logo will receive proper recognition and exposure by donating on either platform.

Documentation

Please see the documentation for full list of available features. Everything listed in the documentation is 100% covered by more than 800 unit tests and should work correctly. Almost all items in the issues page are expected future features and enhancements.

Installation

You can install this library using NuGet into your Test class project. It will automatically reference the needed dependencies of Microsoft.AspNet.WebApi.Core (≥ 5.1.0) and Microsoft.Owin.Testing (≥ 3.0.1) for you. .NET 4.5+ is needed. Make sure your solution has the same versions of the mentioned dependencies in all projects where you are using them. For example, if you are using Microsoft.AspNet.WebApi.Core 5.2.3 in your Web project, the same version should be used after installing MyTested.WebApi in your Tests project.

Install-Package MyTested.WebApi

After the downloading is complete, just add using MyTested.WebApi; and you are ready to test in the most elegant and developer friendly way.

using MyTested.WebApi;

For other interesting packages check out:

How to use

Make sure to check out the documentation for full list of available features. You can also check out the provided samples for real-life ASP.NET Web API application testing.

Basically you can create a test case by using the fluent API the library provides. You are given a static MyWebApi class from which all assertions can be easily configured.

namespace MyApp.Tests.Controllers
{
    using MyTested.WebApi;
	
    using MyApp.Controllers;
    using NUnit.Framework;

    [TestFixture]
    public class HomeControllerShould
    {
        [Test]
        public void ReturnOkWhenCallingGetAction()
        {
            MyWebApi
                .Controller<HomeController>()
                .Calling(c => c.Get())
                .ShouldReturn()
                .Ok();
        }
    }
}

The example uses NUnit but you can use whatever testing framework you want. Basically, MyTested.WebApi throws an unhandled exception if the assertion does not pass and the test fails.

Here are some random examples of what the fluent testing API is capable of:

// tests a route for correct controller, action and resolved route values
MyWebApi
    .Routes()
    .ShouldMap("api/WebApiController/SomeAction/5")
    .WithJsonContent(@"{""SomeInt"": 1, ""SomeString"": ""Test""}")
    .And()
    .WithHttpMethod(HttpMethod.Post)
    .To<WebApiController>(c => c.SomeAction(5, new RequestModel
    {
        SomeInt = 1,
        SomeString = "Test"
    }))
    .AndAlso()
    .ToNoHandler()
    .AndAlso()
    .ToValidModelState();

// injects dependencies into controller
// and mocks authenticated user
// and tests for valid model state
// and tests response model from Ok result with specific assertions
MyWebApi
    .Controller<WebApiController>()
    .WithResolvedDependencyFor<IInjectedService>(mockedInjectedService)
    .WithResolvedDependencyFor<IAnotherInjectedService>(anotherMockedInjectedService);
    .WithAuthenticatedUser(user => user.WithUsername("NewUserName"))
    .Calling(c => c.SomeAction(requestModel))
    .ShouldHave()
    .ValidModelState()
    .AndAlso()
    .ShouldReturn()
    .Ok()
    .WithResponseModelOfType<ResponseModel>()
    .Passing(m =>
    {
        Assert.AreEqual(1, m.Id);
        Assert.AreEqual("Some property value", m.SomeProperty);
    });

// tests whether model state error exists by using lambda expression
// and specific tests for the error messages
MyWebApi
    .Controller<WebApiController>()
    .Calling(c => c.SomeAction(requestModel))
    .ShouldHave()
    .ModelStateFor<RequestModel>()
    .ContainingModelStateErrorFor(m => m.SomeProperty).ThatEquals("Error message") 
    .AndAlso()
    .ContainingModelStateErrorFor(m => m.SecondProperty).BeginningWith("Error") 
    .AndAlso()
    .ContainingModelStateErrorFor(m => m.ThirdProperty).EndingWith("message") 
    .AndAlso()
    .ContainingModelStateErrorFor(m => m.SecondProperty).Containing("ror mes"); 
	
// tests whether the action throws internal server error
// with exception of certain type and with certain message
MyWebApi
    .Controller<WebApiController>()
    .Calling(c => c.SomeAction())
    .

Related Skills

View on GitHub
GitHub Stars752
CategoryDevelopment
Updated2mo ago
Forks81

Languages

C#

Security Score

85/100

Audited on Jan 15, 2026

No findings