Skip to content

Commit

Permalink
Synced development and master
Browse files Browse the repository at this point in the history
Merged development branches
  • Loading branch information
ivaylokenov committed Aug 1, 2019
2 parents 67ce614 + 9bc4385 commit 98b087e
Show file tree
Hide file tree
Showing 30 changed files with 367 additions and 130 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<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; for ASP.NET Web API 2</h1>
<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>
====================================

MyTested.WebApi is 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 the testing library of your choice (e.g. NUnit, xUnit, etc.). Inspired by [ChaiJS](https://github.com/chaijs/chai).
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](https://github.com/chaijs/chai).

[![Build status](https://ci.appveyor.com/api/projects/status/738pm1kuuv7yw1t5?svg=true)](https://ci.appveyor.com/project/ivaylokenov/mywebapi) [![NuGet Version](http://img.shields.io/nuget/v/MyTested.WebApi.svg?style=flat)](https://www.nuget.org/packages/MyTested.WebApi/) [![Coverage Status](https://coveralls.io/repos/ivaylokenov/MyTested.WebApi/badge.svg?branch=master&service=github&v=2)](https://coveralls.io/github/ivaylokenov/MyTested.WebApi?branch=master) [![License](https://img.shields.io/badge/license-apache-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)

Expand All @@ -21,7 +21,9 @@ After the downloading is complete, just add `using MyTested.WebApi;` and you are

For other interesting packages check out:

- [AspNet.Mvc.TypedRouting](https://github.com/ivaylokenov/AspNet.Mvc.TypedRouting) - typed routing and link generation for ASP.NET MVC 6
- [MyTested.AspNetCore.Mvc](https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc) - fluent testing framework for ASP.NET Core MVC
- [MyTested.HttpServer](https://github.com/ivaylokenov/MyTested.HttpServer) - fluent testing framework for remote HTTP servers
- [AspNet.Mvc.TypedRouting](https://github.com/ivaylokenov/AspNet.Mvc.TypedRouting) - typed routing and link generation for ASP.NET Core MVC
- [ASP.NET MVC 5 Lambda Expression Helpers](https://github.com/ivaylokenov/ASP.NET-MVC-Lambda-Expression-Helpers) - typed expression based link generation for ASP.NET MVC 5

## How to use
Expand Down
Binary file removed documentation/MyTested.WebApi.1.2.3.nupkg
Binary file not shown.
Binary file added documentation/MyTested.WebApi.1.2.6.nupkg
Binary file not shown.
Binary file modified documentation/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified documentation/nuget-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class BooksControllerIntegrationTests
private IServerBuilder server;
private string accessToken;

[TestFixtureSetUp]
[OneTimeSetUp]
public void Init()
{
NinjectConfig.RebindAction = kernel =>
Expand Down Expand Up @@ -60,7 +60,7 @@ public void BooksControllerShouldReturnCorrectBooksForAuthorizedUsers()
.Passing(m => m.Count == 10);
}

[TestFixtureTearDown]
[OneTimeTearDown]
public void TearDown()
{
MyWebApi.Server().Stops();
Expand Down
48 changes: 40 additions & 8 deletions samples/Books Web API/Books.Tests/Books.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -74,24 +78,52 @@
<HintPath>..\packages\Microsoft.Owin.Testing.3.0.1\lib\net45\Microsoft.Owin.Testing.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.2.1510.2205, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
<Reference Include="Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit3TestAdapter.3.4.1\lib\Mono.Cecil.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Cecil.Mdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit3TestAdapter.3.4.1\lib\Mono.Cecil.Mdb.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Cecil.Pdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit3TestAdapter.3.4.1\lib\Mono.Cecil.Pdb.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Cecil.Rocks, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit3TestAdapter.3.4.1\lib\Mono.Cecil.Rocks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MyTested.WebApi, Version=1.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MyTested.WebApi.1.2.3\lib\net45\MyTested.WebApi.dll</HintPath>
<Reference Include="Moq, Version=4.5.21.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.5.21\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="MyTested.WebApi, Version=1.2.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MyTested.WebApi.1.2.5\lib\net45\MyTested.WebApi.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
<HintPath>..\packages\Ninject.3.2.2.0\lib\net45-full\Ninject.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Reference Include="nunit.engine, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit3TestAdapter.3.4.1\lib\nunit.engine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.engine.api, Version=3.0.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit3TestAdapter.3.4.1\lib\nunit.engine.api.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.4.1\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NUnit3.TestAdapter, Version=3.4.1.0, Culture=neutral, PublicKeyToken=4cb40d35494691ac, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit3TestAdapter.3.4.1\lib\NUnit3.TestAdapter.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
Expand Down
2 changes: 1 addition & 1 deletion samples/Books Web API/Books.Tests/TestsStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[SetUpFixture]
public class TestsStartup
{
[SetUp]
[OneTimeSetUp]
public void SetUpTests()
{
AutoMapperConfig.RegisterMappings(Assembly.Load("Books.Api"));
Expand Down
2 changes: 1 addition & 1 deletion samples/Books Web API/Books.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
Expand Down
10 changes: 6 additions & 4 deletions samples/Books Web API/Books.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="3.3.3" targetFramework="net45" />
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net45" />
Expand All @@ -12,10 +13,11 @@
<package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Testing" version="3.0.1" targetFramework="net45" />
<package id="Moq" version="4.2.1510.2205" targetFramework="net45" />
<package id="MyTested.WebApi" version="1.2.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
<package id="Moq" version="4.5.21" targetFramework="net45" />
<package id="MyTested.WebApi" version="1.2.5" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="Ninject" version="3.2.2.0" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
<package id="NUnit" version="3.4.1" targetFramework="net45" />
<package id="NUnit3TestAdapter" version="3.4.1" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,109 @@

public static class AutoMapperConfig
{
private static TypeInfo ProfileTypeInfo = typeof(Profile).GetTypeInfo();

public static void RegisterMappings(params Assembly[] assemblies)
{
var types = new List<Type>();
foreach (var assembly in assemblies)
var allTypes = assemblies.SelectMany(a => a.GetExportedTypes().Select(t => t.GetTypeInfo()));

var profiles = allTypes
.Where(t => ProfileTypeInfo.IsAssignableFrom(t))
.Where(t => !t.IsAbstract);

Mapper.Initialize(configuration =>
{
types.AddRange(assembly.GetExportedTypes());
}
configuration.AddProfile(StandardMappingsProfile.From(allTypes));
configuration.AddProfile(CustomMappingsProfile.From(allTypes));
LoadStandardMappings(types);
LoadCustomMappings(types);
foreach (var profile in profiles)
{
configuration.AddProfile(profile);
}
});
}

private static void LoadStandardMappings(IEnumerable<Type> types)
private class StandardMappingsProfile : Profile
{
var maps = types.SelectMany(t => t.GetInterfaces(), (t, i) => new { t, i })
.Where(
type =>
type.i.IsGenericType && type.i.GetGenericTypeDefinition() == typeof(IMapFrom<>) &&
!type.t.IsAbstract
&& !type.t.IsInterface)
.Select(type => new { Source = type.i.GetGenericArguments()[0], Destination = type.t });

foreach (var map in maps)
private static Type MapFromType = typeof(IMapFrom<>);

private readonly IEnumerable<TypeInfo> types;

public static StandardMappingsProfile From(IEnumerable<TypeInfo> types)
=> new StandardMappingsProfile(types);

private StandardMappingsProfile(IEnumerable<TypeInfo> types)
{
Mapper.CreateMap(map.Source, map.Destination);
Mapper.CreateMap(map.Destination, map.Source);
this.types = types;
this.LoadMappings();
}

private void LoadMappings()
{
var maps = this.types
.SelectMany(
t => t.GetInterfaces(),
(t, i) => new
{
Type = t,
Interface = i.GetTypeInfo()
})
.Where(
map =>
map.Interface.IsGenericType
&& map.Interface.GetGenericTypeDefinition() == MapFromType
&& !map.Type.IsAbstract
&& !map.Type.IsInterface)
.Select(type => new
{
Source = type.Interface.GetGenericArguments()[0],
Destination = type.Type.AsType()
});

foreach (var map in maps)
{
CreateMap(map.Source, map.Destination);
CreateMap(map.Destination, map.Source);
}
}
}

private static void LoadCustomMappings(IEnumerable<Type> types)
private class CustomMappingsProfile : Profile
{
var maps =
types.SelectMany(t => t.GetInterfaces(), (t, i) => new { t, i })
.Where(
type =>
typeof(IHaveCustomMappings).IsAssignableFrom(type.t) && !type.t.IsAbstract &&
!type.t.IsInterface)
.Select(type => (IHaveCustomMappings)Activator.CreateInstance(type.t));
private static TypeInfo CustomMappingsTypeInfo = typeof(IHaveCustomMappings).GetTypeInfo();

private readonly IEnumerable<TypeInfo> types;

public static CustomMappingsProfile From(IEnumerable<TypeInfo> types)
=> new CustomMappingsProfile(types);

foreach (var map in maps)
private CustomMappingsProfile(IEnumerable<TypeInfo> types)
{
map.CreateMappings(Mapper.Configuration);
this.types = types;
this.LoadMappings();
}

private void LoadMappings()
{
var maps = this.types
.SelectMany(
t => t.GetInterfaces(),
(t, i) => new
{
Type = t,
Interface = i.GetTypeInfo()
})
.Where(
map =>
CustomMappingsTypeInfo.IsAssignableFrom(map.Type)
&& !map.Type.IsAbstract
&& !map.Type.IsInterface)
.Select(type => (IHaveCustomMappings)Activator.CreateInstance(type.Type.AsType()));

foreach (var map in maps)
{
map.CreateMappings(this);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface IHaveCustomMappings
{
void CreateMappings(IConfiguration config);
void CreateMappings(IProfileExpression mapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ListedCommitResponseModel : IMapFrom<Commit>, IHaveCustomMappings

public DateTime CreatedOn { get; set; }

public void CreateMappings(IConfiguration config)
public void CreateMappings(IProfileExpression config)
{
config.CreateMap<Commit, ListedCommitResponseModel>()
.ForMember(c => c.UserName, opt => opt.MapFrom(c => c.User.UserName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SoftwareProjectDetailsResponseModel : IMapFrom<SoftwareProject>, IH

public int TotalUsers { get; set; }

public void CreateMappings(IConfiguration config)
public void CreateMappings(IProfileExpression config)
{
config.CreateMap<SoftwareProject, SoftwareProjectDetailsResponseModel>()
.ForMember(s => s.TotalUsers, opts => opts.MapFrom(s => s.Users.Count()));
Expand Down
Loading

0 comments on commit 98b087e

Please sign in to comment.