Skip to content

Commit 9dcd045

Browse files
committed
Added Lite package which does not need license (closes #229)
1 parent 21293d4 commit 9dcd045

File tree

22 files changed

+506
-398
lines changed

22 files changed

+506
-398
lines changed

README.md

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,34 @@ It is strongly advised to start with the [tutorial](http://docs.mytestedasp.net/
1111

1212
## Installation
1313

14-
You can install this library using NuGet into your test project (or reference it directly in your `project.json` file). Currently MyTested.AspNetCore.Mvc works with ASP.NET Core MVC 1.0.0.
14+
You can install this library using NuGet into your test project (or reference it directly in your `project.json` file). Currently `MyTested.AspNetCore.Mvc` works with ASP.NET Core MVC 1.0.0.
1515

1616
Install-Package MyTested.AspNetCore.Mvc.Universe
1717

18-
This package will include all available assertion methods in your test project, including ones for authentication, database, session, caching and more. If you want only the MVC related features, install `MyTested.AspNetCore.Mvc`. Additionally, if you prefer, you can be more specific by including only some of the packages:
19-
20-
- `MyTested.AspNetCore.Mvc.Controllers` - contains setup and assertion methods for controllers
21-
- `MyTested.AspNetCore.Mvc.Routing` - contains setup and assertion methods for routes
22-
- `MyTested.AspNetCore.Mvc.Core` - contains setup and assertion methods for MVC core features
23-
- `MyTested.AspNetCore.Mvc.TempData` - contains setup and assertion methods for `ITempDataDictionary`
24-
- `MyTested.AspNetCore.Mvc.ViewData` - contains assertion methods for `ViewDataDictionary` and dynamic `ViewBag`
25-
- `MyTested.AspNetCore.Mvc.ViewActionResults` - contains setup and assertion methods for view action results
26-
- `MyTested.AspNetCore.Mvc.ViewComponents` - contains setup and assertion methods for view components
27-
- `MyTested.AspNetCore.Mvc.ViewFeatures` - contains setup and assertion methods for MVC view features
28-
- `MyTested.AspNetCore.Mvc.Http` - contains setup and assertion methods for HTTP context, request and response
29-
- `MyTested.AspNetCore.Mvc.Authentication` - contains setup methods for `ClaimsPrincipal`
30-
- `MyTested.AspNetCore.Mvc.ModelState` - contains setup and assertion methods for `ModelStateDictionary` validations
31-
- `MyTested.AspNetCore.Mvc.DataAnnotations` - contains setup and assertion methods for data annotation validations
32-
- `MyTested.AspNetCore.Mvc.EntityFrameworkCore` - contains setup and assertion methods for `DbContext`
33-
- `MyTested.AspNetCore.Mvc.DependencyInjection` - contains setup methods for dependency injection services
34-
- `MyTested.AspNetCore.Mvc.Caching` - contains setup and assertion methods for `IMemoryCache`
35-
- `MyTested.AspNetCore.Mvc.Session` - contains setup and assertion methods for `ISession`
36-
- `MyTested.AspNetCore.Mvc.Options` - contains setup and assertion methods for `IOptions`
37-
18+
This package will include all available assertion methods in your test project, including ones for authentication, database, session, caching and more. If you want only the MVC related features, install `MyTested.AspNetCore.Mvc`. If you want to use the completely **FREE** and **UNLIMITED** version of the library, install only `MyTested.AspNetCore.Mvc.Lite` and no other package. Additionally, if you prefer, you can be more specific by including only some of the packages:
19+
20+
- `MyTested.AspNetCore.Mvc.Controllers` - Contains setup and assertion methods for controllers
21+
- `MyTested.AspNetCore.Mvc.Models` - Contains setup and assertion methods for response and view models
22+
- `MyTested.AspNetCore.Mvc.Routing` - Contains setup and assertion methods for routes
23+
- `MyTested.AspNetCore.Mvc.Core` - Contains setup and assertion methods for MVC core features
24+
- `MyTested.AspNetCore.Mvc.TempData` - Contains setup and assertion methods for `ITempDataDictionary`
25+
- `MyTested.AspNetCore.Mvc.ViewData` - Contains assertion methods for `ViewDataDictionary` and dynamic `ViewBag`
26+
- `MyTested.AspNetCore.Mvc.ViewActionResults` - Contains setup and assertion methods for view action results
27+
- `MyTested.AspNetCore.Mvc.ViewComponents` - Contains setup and assertion methods for view components
28+
- `MyTested.AspNetCore.Mvc.ViewFeatures` - Contains setup and assertion methods for MVC view features
29+
- `MyTested.AspNetCore.Mvc.Http` - Contains setup and assertion methods for HTTP context, request and response
30+
- `MyTested.AspNetCore.Mvc.Authentication` - Contains setup methods for `ClaimsPrincipal`
31+
- `MyTested.AspNetCore.Mvc.ModelState` - Contains setup and assertion methods for `ModelStateDictionary` validations
32+
- `MyTested.AspNetCore.Mvc.DataAnnotations` - Contains setup and assertion methods for data annotation validations
33+
- `MyTested.AspNetCore.Mvc.EntityFrameworkCore` - Contains setup and assertion methods for `DbContext`
34+
- `MyTested.AspNetCore.Mvc.DependencyInjection` - Contains setup methods for dependency injection services
35+
- `MyTested.AspNetCore.Mvc.Caching` - Contains setup and assertion methods for `IMemoryCache`
36+
- `MyTested.AspNetCore.Mvc.Session` - Contains setup and assertion methods for `ISession`
37+
- `MyTested.AspNetCore.Mvc.Options` - Contains setup and assertion methods for `IOptions`
38+
- `MyTested.AspNetCore.Mvc.Helpers` - Contains additional helper methods for easier assertions
39+
40+
- `MyTested.AspNetCore.Mvc.Lite` - Completely **FREE** and **UNLIMITED** version of the library. It should not be used in combination with any other package. Includes `Controllers`, `ViewActionResults` and `ViewComponents`.
41+
3842
After the downloading is complete, just add `using MyTested.AspNetCore.Mvc;` to your source code and you are ready to test in the most elegant and developer friendly way.
3943

4044
using MyTested.AspNetCore.Mvc;
@@ -215,6 +219,24 @@ MyMvc
215219
.OfType<NullReferenceException>()
216220
.AndAlso()
217221
.WithMessage().ThatEquals("Test exception message");
222+
223+
// all applicable methods are available
224+
// on view component testing too
225+
MyMvc
226+
.ViewComponent<MvcComponent>()
227+
.WithSession(session => session
228+
.WithEntry("Session", "SessionValue"))
229+
.WithDbContext(db => db.WithEntities(entities => entities
230+
.AddRange(SampleDataProvider.GetModels())))
231+
.InvokedWith(c => c.InvokeAsync(1))
232+
.ShouldHave()
233+
.ViewBag(viewBag => viewBag
234+
.ContainingEntry("TotalItems", 10)
235+
.ContainingEntry("EntryName", "ViewBagName"))
236+
.AndAlso()
237+
.ShouldReturn()
238+
.View()
239+
.WithModelOfType<ResponseModel>();
218240
```
219241

220242
## License

samples/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Here you can find a few working samples, which will get you started with the lib
55

66
## Application Samples
77

8-
- MusicStore - sample showing how to use MyTested.AspNetCore.Mvc with an automatically resolved `TestStartup` class for unit testing controllers, view components and routes. It uses the [official ASP.NET Core MVC sample](https://github.com/aspnet/MusicStore) and [xUnit](http://xunit.github.io/).
8+
- **MusicStore** - sample showing how to use MyTested.AspNetCore.Mvc with an automatically resolved `TestStartup` class for unit testing controllers, view components and routes. It uses the [official ASP.NET Core MVC sample](https://github.com/aspnet/MusicStore) and [xUnit](http://xunit.github.io/).
99

1010
## Functional Test Samples
1111

12-
- ApplicationParts - minimalistic functional sample testing whether controllers are found correctly when registered from external assemblies. Uses manual `Startup` configuration and [NUnit](https://github.com/nunit/dotnet-test-nunit).
13-
- NoStartup - minimalistic functional sample showing how to use the testing library without any globally configured `Startup` class. Uses [MSTest](https://blogs.msdn.microsoft.com/visualstudioalm/2016/09/01/announcing-mstest-v2-framework-support-for-net-core-1-0-rtm/).
12+
- **ApplicationParts** - minimalistic functional sample testing whether controllers are found correctly when registered from external assemblies. Uses manual `Startup` configuration and [NUnit](https://github.com/nunit/dotnet-test-nunit).
13+
- **NoStartup** - minimalistic functional sample showing how to use the testing library without any globally configured `Startup` class. Uses [MSTest](https://blogs.msdn.microsoft.com/visualstudioalm/2016/09/01/announcing-mstest-v2-framework-support-for-net-core-1-0-rtm/).
14+
- **Lite** - minimalistic functional sample showing how to use the completely **FREE** and **UNLIMITED** version of the library - `MyTested.AspNetCore.Mvc.Lite`. Tests API controllers by using automatically resolved `TestStartup` class and [Moq](https://github.com/moq/moq4).

src/MyTested.AspNetCore.Mvc.Controllers/Builders/ActionResults/Created/CreatedTestBuilder.cs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq.Expressions;
65
using System.Net;
7-
using System.Threading.Tasks;
86
using Base;
97
using Contracts.ActionResults.Created;
108
using Contracts.Uri;
@@ -30,9 +28,7 @@ public class CreatedTestBuilder<TCreatedResult>
3028
private const string RouteName = "route name";
3129
private const string RouteValues = "route values";
3230
private const string UrlHelper = "URL helper";
33-
34-
private LambdaExpression createdAtExpression;
35-
31+
3632
/// <summary>
3733
/// Initializes a new instance of the <see cref="CreatedTestBuilder{TCreatedResult}"/> class.
3834
/// </summary>
@@ -44,6 +40,8 @@ public CreatedTestBuilder(ControllerTestContext testContext)
4440
this.controllerTestContext = testContext;
4541
}
4642

43+
public bool IncludeCountCheck { get; set; } = true;
44+
4745
/// <inheritdoc />
4846
public IAndCreatedTestBuilder WithStatusCode(int statusCode)
4947
{
@@ -283,12 +281,10 @@ public IAndCreatedTestBuilder ContainingRouteValues(object routeValues)
283281
/// <inheritdoc />
284282
public IAndCreatedTestBuilder ContainingRouteValues(IDictionary<string, object> routeValues)
285283
{
286-
var includeCountCheck = this.createdAtExpression == null;
287-
288284
RouteActionResultValidator.ValidateRouteValues(
289285
this.TestContext.MethodResult,
290286
routeValues,
291-
includeCountCheck,
287+
this.IncludeCountCheck,
292288
this.ThrowNewCreatedResultAssertionException);
293289

294290
return this;
@@ -316,20 +312,6 @@ public IAndCreatedTestBuilder WithUrlHelperOfType<TUrlHelper>()
316312
return this;
317313
}
318314

319-
/// <inheritdoc />
320-
public IAndCreatedTestBuilder At<TController>(Expression<Action<TController>> actionCall)
321-
where TController : class
322-
{
323-
return this.ProcessRouteLambdaExpression<TController>(actionCall);
324-
}
325-
326-
/// <inheritdoc />
327-
public IAndCreatedTestBuilder At<TController>(Expression<Func<TController, Task>> actionCall)
328-
where TController : class
329-
{
330-
return this.ProcessRouteLambdaExpression<TController>(actionCall);
331-
}
332-
333315
/// <inheritdoc />
334316
public ICreatedTestBuilder AndAlso() => this;
335317

@@ -357,20 +339,7 @@ private TExpectedCreatedResult GetCreatedResult<TExpectedCreatedResult>(string c
357339
return actualRedirectResult;
358340
}
359341

360-
private IAndCreatedTestBuilder ProcessRouteLambdaExpression<TController>(LambdaExpression actionCall)
361-
{
362-
this.createdAtExpression = actionCall;
363-
364-
RouteActionResultValidator.ValidateExpressionLink(
365-
this.controllerTestContext,
366-
LinkGenerationTestContext.FromCreatedResult(this.TestContext.MethodResult as IActionResult),
367-
actionCall,
368-
this.ThrowNewCreatedResultAssertionException);
369-
370-
return this;
371-
}
372-
373-
private void ThrowNewCreatedResultAssertionException(string propertyName, string expectedValue, string actualValue)
342+
public void ThrowNewCreatedResultAssertionException(string propertyName, string expectedValue, string actualValue)
374343
{
375344
throw new CreatedResultAssertionException(string.Format(
376345
"{0} created result {1} {2}, but {3}.",

src/MyTested.AspNetCore.Mvc.Controllers/Builders/ActionResults/LocalRedirect/LocalRedirectTestBuilder.cs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.ActionResults.LocalRedirect
22
{
33
using System;
4-
using System.Linq.Expressions;
5-
using System.Threading.Tasks;
64
using Base;
75
using Contracts.ActionResults.LocalRedirect;
86
using Contracts.Uri;
@@ -117,35 +115,10 @@ public IAndLocalRedirectTestBuilder WithUrlHelperOfType<TUrlHelper>()
117115
return this;
118116
}
119117

120-
/// <inheritdoc />
121-
public IAndLocalRedirectTestBuilder To<TController>(Expression<Action<TController>> actionCall)
122-
where TController : class
123-
{
124-
return this.ProcessRouteLambdaExpression<TController>(actionCall);
125-
}
126-
127-
/// <inheritdoc />
128-
public IAndLocalRedirectTestBuilder To<TController>(Expression<Func<TController, Task>> actionCall)
129-
where TController : class
130-
{
131-
return this.ProcessRouteLambdaExpression<TController>(actionCall);
132-
}
133-
134118
/// <inheritdoc />
135119
public ILocalRedirectTestBuilder AndAlso() => this;
136120

137-
private IAndLocalRedirectTestBuilder ProcessRouteLambdaExpression<TController>(LambdaExpression actionCall)
138-
{
139-
RouteActionResultValidator.ValidateExpressionLink(
140-
this.TestContext,
141-
LinkGenerationTestContext.FromLocalRedirectResult(this.ActionResult),
142-
actionCall,
143-
this.ThrowNewRedirectResultAssertionException);
144-
145-
return this;
146-
}
147-
148-
private void ThrowNewRedirectResultAssertionException(string propertyName, string expectedValue, string actualValue)
121+
public void ThrowNewRedirectResultAssertionException(string propertyName, string expectedValue, string actualValue)
149122
{
150123
throw new RedirectResultAssertionException(string.Format(
151124
"When calling {0} action in {1} expected local redirect result {2} {3}, but {4}.",

src/MyTested.AspNetCore.Mvc.Controllers/Builders/ActionResults/Redirect/RedirectTestBuilder.cs

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq.Expressions;
6-
using System.Threading.Tasks;
75
using Base;
86
using Contracts.ActionResults.Redirect;
9-
using Contracts.Base;
107
using Contracts.Uri;
118
using Exceptions;
129
using Internal.TestContexts;
@@ -25,9 +22,7 @@ public class RedirectTestBuilder<TRedirectResult>
2522
{
2623
private const string Location = "location";
2724
private const string RouteName = "route name";
28-
29-
private LambdaExpression redirectToExpression;
30-
25+
3126
/// <summary>
3227
/// Initializes a new instance of the <see cref="RedirectTestBuilder{TRedirectResult}"/> class.
3328
/// </summary>
@@ -37,6 +32,8 @@ public RedirectTestBuilder(ControllerTestContext testContext)
3732
{
3833
}
3934

35+
public bool IncludeCountCheck { get; set; } = true;
36+
4037
/// <inheritdoc />
4138
public IAndRedirectTestBuilder Permanent()
4239
{
@@ -208,12 +205,10 @@ public IAndRedirectTestBuilder ContainingRouteValues(object routeValues)
208205
/// <inheritdoc />
209206
public IAndRedirectTestBuilder ContainingRouteValues(IDictionary<string, object> routeValues)
210207
{
211-
var includeCountCheck = this.redirectToExpression == null;
212-
213208
RouteActionResultValidator.ValidateRouteValues(
214209
this.ActionResult,
215210
routeValues,
216-
includeCountCheck,
211+
this.IncludeCountCheck,
217212
this.ThrowNewRedirectResultAssertionException);
218213

219214
return this;
@@ -241,20 +236,6 @@ public IAndRedirectTestBuilder WithUrlHelperOfType<TUrlHelper>()
241236
return this;
242237
}
243238

244-
/// <inheritdoc />
245-
public IAndRedirectTestBuilder To<TController>(Expression<Action<TController>> actionCall)
246-
where TController : class
247-
{
248-
return this.ProcessRouteLambdaExpression<TController>(actionCall);
249-
}
250-
251-
/// <inheritdoc />
252-
public IAndRedirectTestBuilder To<TController>(Expression<Func<TController, Task>> actionCall)
253-
where TController : class
254-
{
255-
return this.ProcessRouteLambdaExpression<TController>(actionCall);
256-
}
257-
258239
/// <inheritdoc />
259240
public IRedirectTestBuilder AndAlso() => this;
260241

@@ -273,20 +254,7 @@ private TExpectedRedirectResult GetRedirectResult<TExpectedRedirectResult>(strin
273254
return actualRedirectResult;
274255
}
275256

276-
private IAndRedirectTestBuilder ProcessRouteLambdaExpression<TController>(LambdaExpression actionCall)
277-
{
278-
this.redirectToExpression = actionCall;
279-
280-
RouteActionResultValidator.ValidateExpressionLink(
281-
this.TestContext,
282-
LinkGenerationTestContext.FromRedirectResult(this.ActionResult),
283-
actionCall,
284-
this.ThrowNewRedirectResultAssertionException);
285-
286-
return this;
287-
}
288-
289-
private void ThrowNewRedirectResultAssertionException(string propertyName, string expectedValue, string actualValue)
257+
public void ThrowNewRedirectResultAssertionException(string propertyName, string expectedValue, string actualValue)
290258
{
291259
throw new RedirectResultAssertionException(string.Format(
292260
"When calling {0} action in {1} expected redirect result {2} {3}, but {4}.",

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Contracts/ActionResults/Created/ICreatedTestBuilder.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq.Expressions;
65
using System.Net;
7-
using System.Threading.Tasks;
86
using Base;
97
using Microsoft.AspNetCore.Mvc;
108
using Microsoft.AspNetCore.Mvc.Formatters;
@@ -139,25 +137,7 @@ public interface ICreatedTestBuilder : IBaseTestBuilderWithResponseModel,
139137
/// <returns>The same <see cref="IAndCreatedTestBuilder"/>.</returns>
140138
IAndCreatedTestBuilder WithUrlHelperOfType<TUrlHelper>()
141139
where TUrlHelper : IUrlHelper;
142-
143-
/// <summary>
144-
/// Tests whether <see cref="CreatedAtActionResult"/> or <see cref="CreatedAtRouteResult"/> returns created at specific action.
145-
/// </summary>
146-
/// <typeparam name="TController">Type of expected controller.</typeparam>
147-
/// <param name="actionCall">Method call expression indicating the expected action.</param>
148-
/// <returns>The same <see cref="IAndCreatedTestBuilder"/>.</returns>
149-
IAndCreatedTestBuilder At<TController>(Expression<Action<TController>> actionCall)
150-
where TController : class;
151-
152-
/// <summary>
153-
/// Tests whether <see cref="CreatedAtActionResult"/> or <see cref="CreatedAtRouteResult"/> returns created at specific asynchronous action.
154-
/// </summary>
155-
/// <typeparam name="TController">Type of expected controller.</typeparam>
156-
/// <param name="actionCall">Method call expression indicating the expected asynchronous created action.</param>
157-
/// <returns>The same <see cref="IAndCreatedTestBuilder"/>.</returns>
158-
IAndCreatedTestBuilder At<TController>(Expression<Func<TController, Task>> actionCall)
159-
where TController : class;
160-
140+
161141
/// <summary>
162142
/// Tests whether <see cref="CreatedResult"/>, <see cref="CreatedAtActionResult"/> or <see cref="CreatedAtRouteResult"/> has the same status code as the provided one.
163143
/// </summary>

0 commit comments

Comments
 (0)