Skip to content

Commit 13a379b

Browse files
committed
Added MyTested.AspNetCore.Mvc.DependencyInjection project (#125)
1 parent 28a00dc commit 13a379b

File tree

32 files changed

+7709
-184
lines changed

32 files changed

+7709
-184
lines changed

MyTested.AspNetCore.Mvc.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Aut
8080
EndProject
8181
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Controllers", "src\MyTested.AspNetCore.Mvc.Controllers\MyTested.AspNetCore.Mvc.Controllers.xproj", "{9B2A773E-3A37-44F3-83D0-5D156526FA84}"
8282
EndProject
83+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.DependencyInjection", "src\MyTested.AspNetCore.Mvc.DependencyInjection\MyTested.AspNetCore.Mvc.DependencyInjection.xproj", "{C15BB589-9421-4D9C-A26D-61FF1BCD17AC}"
84+
EndProject
8385
Global
8486
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8587
Debug|Any CPU = Debug|Any CPU
@@ -210,6 +212,10 @@ Global
210212
{9B2A773E-3A37-44F3-83D0-5D156526FA84}.Debug|Any CPU.Build.0 = Debug|Any CPU
211213
{9B2A773E-3A37-44F3-83D0-5D156526FA84}.Release|Any CPU.ActiveCfg = Release|Any CPU
212214
{9B2A773E-3A37-44F3-83D0-5D156526FA84}.Release|Any CPU.Build.0 = Release|Any CPU
215+
{C15BB589-9421-4D9C-A26D-61FF1BCD17AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
216+
{C15BB589-9421-4D9C-A26D-61FF1BCD17AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
217+
{C15BB589-9421-4D9C-A26D-61FF1BCD17AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
218+
{C15BB589-9421-4D9C-A26D-61FF1BCD17AC}.Release|Any CPU.Build.0 = Release|Any CPU
213219
EndGlobalSection
214220
GlobalSection(SolutionProperties) = preSolution
215221
HideSolutionNode = FALSE
@@ -248,5 +254,6 @@ Global
248254
{2101DDC1-36A4-4D4E-A4D8-EF24D47116A4} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
249255
{18B6C20B-9C4B-4444-AD61-8DAF57012599} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
250256
{9B2A773E-3A37-44F3-83D0-5D156526FA84} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
257+
{C15BB589-9421-4D9C-A26D-61FF1BCD17AC} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
251258
EndGlobalSection
252259
EndGlobal

samples/ApplicationParts/ApplicationParts.Test/project.lock.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4183,6 +4183,7 @@
41834183
"MyTested.AspNetCore.Mvc.Authentication": "1.0.0",
41844184
"MyTested.AspNetCore.Mvc.Caching": "1.0.0",
41854185
"MyTested.AspNetCore.Mvc.Core": "1.0.0",
4186+
"MyTested.AspNetCore.Mvc.DependencyInjection": "1.0.0",
41864187
"MyTested.AspNetCore.Mvc.EntityFrameworkCore": "1.0.0",
41874188
"MyTested.AspNetCore.Mvc.Options": "1.0.0",
41884189
"MyTested.AspNetCore.Mvc.Routing": "1.0.0",
@@ -4282,6 +4283,19 @@
42824283
"netstandard1.6/MyTested.AspNetCore.Mvc.DataAnnotations.dll": {}
42834284
}
42844285
},
4286+
"MyTested.AspNetCore.Mvc.DependencyInjection/1.0.0-preview-final": {
4287+
"type": "project",
4288+
"framework": ".NETStandard,Version=v1.6",
4289+
"dependencies": {
4290+
"MyTested.AspNetCore.Mvc.Abstractions": "1.0.0"
4291+
},
4292+
"compile": {
4293+
"netstandard1.6/MyTested.AspNetCore.Mvc.DependencyInjection.dll": {}
4294+
},
4295+
"runtime": {
4296+
"netstandard1.6/MyTested.AspNetCore.Mvc.DependencyInjection.dll": {}
4297+
}
4298+
},
42854299
"MyTested.AspNetCore.Mvc.EntityFrameworkCore/1.0.0-preview-final": {
42864300
"type": "project",
42874301
"framework": ".NETStandard,Version=v1.6",
@@ -10882,6 +10896,11 @@
1088210896
"path": "../../../src/MyTested.AspNetCore.Mvc.DataAnnotations/project.json",
1088310897
"msbuildProject": "../../../src/MyTested.AspNetCore.Mvc.DataAnnotations/MyTested.AspNetCore.Mvc.DataAnnotations.xproj"
1088410898
},
10899+
"MyTested.AspNetCore.Mvc.DependencyInjection/1.0.0-preview-final": {
10900+
"type": "project",
10901+
"path": "../../../src/MyTested.AspNetCore.Mvc.DependencyInjection/project.json",
10902+
"msbuildProject": "../../../src/MyTested.AspNetCore.Mvc.DependencyInjection/MyTested.AspNetCore.Mvc.DependencyInjection.xproj"
10903+
},
1088510904
"MyTested.AspNetCore.Mvc.EntityFrameworkCore/1.0.0-preview-final": {
1088610905
"type": "project",
1088710906
"path": "../../../src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/project.json",

samples/MusicStore/MusicStore.Test/Controllers/StoreControllerTest.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public void IndexShouldGenerateViewWithGenres()
1818
MyMvc
1919
.Controller<StoreController>()
2020
.WithDbContext(db => db
21-
.WithEntities(entities => CreateTestGenres(numberOfGenres: 10, numberOfAlbums: 1, dbContext: entities)))
21+
.WithEntities(entities => CreateTestGenres(
22+
numberOfGenres: 10,
23+
numberOfAlbums: 1,
24+
dbContext: entities)))
2225
.Calling(c => c.Index())
2326
.ShouldReturn()
2427
.View()
@@ -44,7 +47,10 @@ public void BrowseShouldReturnViewGenre()
4447
MyMvc
4548
.Controller<StoreController>()
4649
.WithDbContext(db => db
47-
.WithEntities(entities => CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: entities)))
50+
.WithEntities(entities => CreateTestGenres(
51+
numberOfGenres: 3,
52+
numberOfAlbums: 3,
53+
dbContext: entities)))
4854
.Calling(c => c.Browse(genre))
4955
.ShouldReturn()
5056
.View()
@@ -78,7 +84,10 @@ public void DetailsShouldAlbumDetails()
7884
.WithOptions(options => options
7985
.For<AppSettings>(settings => settings.CacheDbResults = true))
8086
.WithDbContext(db => db
81-
.WithEntities(entities => genres = CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: entities)))
87+
.WithEntities(entities => genres = CreateTestGenres(
88+
numberOfGenres: 3,
89+
numberOfAlbums: 3,
90+
dbContext: entities)))
8291
.Calling(c => c.Details(From.Services<IMemoryCache>(), 1))
8392
.ShouldHave()
8493
.MemoryCache(cache => cache

samples/MusicStore/MusicStore.Test/project.lock.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,6 +4326,7 @@
43264326
"MyTested.AspNetCore.Mvc.Authentication": "1.0.0",
43274327
"MyTested.AspNetCore.Mvc.Caching": "1.0.0",
43284328
"MyTested.AspNetCore.Mvc.Core": "1.0.0",
4329+
"MyTested.AspNetCore.Mvc.DependencyInjection": "1.0.0",
43294330
"MyTested.AspNetCore.Mvc.EntityFrameworkCore": "1.0.0",
43304331
"MyTested.AspNetCore.Mvc.Options": "1.0.0",
43314332
"MyTested.AspNetCore.Mvc.Routing": "1.0.0",
@@ -4425,6 +4426,19 @@
44254426
"netstandard1.6/MyTested.AspNetCore.Mvc.DataAnnotations.dll": {}
44264427
}
44274428
},
4429+
"MyTested.AspNetCore.Mvc.DependencyInjection/1.0.0-preview-final": {
4430+
"type": "project",
4431+
"framework": ".NETStandard,Version=v1.6",
4432+
"dependencies": {
4433+
"MyTested.AspNetCore.Mvc.Abstractions": "1.0.0"
4434+
},
4435+
"compile": {
4436+
"netstandard1.6/MyTested.AspNetCore.Mvc.DependencyInjection.dll": {}
4437+
},
4438+
"runtime": {
4439+
"netstandard1.6/MyTested.AspNetCore.Mvc.DependencyInjection.dll": {}
4440+
}
4441+
},
44284442
"MyTested.AspNetCore.Mvc.EntityFrameworkCore/1.0.0-preview-final": {
44294443
"type": "project",
44304444
"framework": ".NETStandard,Version=v1.6",
@@ -6717,6 +6731,7 @@
67176731
"MyTested.AspNetCore.Mvc.Authentication": "1.0.0",
67186732
"MyTested.AspNetCore.Mvc.Caching": "1.0.0",
67196733
"MyTested.AspNetCore.Mvc.Core": "1.0.0",
6734+
"MyTested.AspNetCore.Mvc.DependencyInjection": "1.0.0",
67206735
"MyTested.AspNetCore.Mvc.EntityFrameworkCore": "1.0.0",
67216736
"MyTested.AspNetCore.Mvc.Options": "1.0.0",
67226737
"MyTested.AspNetCore.Mvc.Routing": "1.0.0",
@@ -6816,6 +6831,19 @@
68166831
"net451/MyTested.AspNetCore.Mvc.DataAnnotations.dll": {}
68176832
}
68186833
},
6834+
"MyTested.AspNetCore.Mvc.DependencyInjection/1.0.0-preview-final": {
6835+
"type": "project",
6836+
"framework": ".NETFramework,Version=v4.5.1",
6837+
"dependencies": {
6838+
"MyTested.AspNetCore.Mvc.Abstractions": "1.0.0"
6839+
},
6840+
"compile": {
6841+
"net451/MyTested.AspNetCore.Mvc.DependencyInjection.dll": {}
6842+
},
6843+
"runtime": {
6844+
"net451/MyTested.AspNetCore.Mvc.DependencyInjection.dll": {}
6845+
}
6846+
},
68196847
"MyTested.AspNetCore.Mvc.EntityFrameworkCore/1.0.0-preview-final": {
68206848
"type": "project",
68216849
"framework": ".NETFramework,Version=v4.5.1",
@@ -13551,6 +13579,11 @@
1355113579
"path": "../../../src/MyTested.AspNetCore.Mvc.DataAnnotations/project.json",
1355213580
"msbuildProject": "../../../src/MyTested.AspNetCore.Mvc.DataAnnotations/MyTested.AspNetCore.Mvc.DataAnnotations.xproj"
1355313581
},
13582+
"MyTested.AspNetCore.Mvc.DependencyInjection/1.0.0-preview-final": {
13583+
"type": "project",
13584+
"path": "../../../src/MyTested.AspNetCore.Mvc.DependencyInjection/project.json",
13585+
"msbuildProject": "../../../src/MyTested.AspNetCore.Mvc.DependencyInjection/MyTested.AspNetCore.Mvc.DependencyInjection.xproj"
13586+
},
1355413587
"MyTested.AspNetCore.Mvc.EntityFrameworkCore/1.0.0-preview-final": {
1355513588
"type": "project",
1355613589
"path": "../../../src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/project.json",

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Base/BaseTestBuilderWithComponent.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Base
22
{
33
using System;
4+
using System.Collections.Generic;
45
using Contracts.Base;
56
using Internal.TestContexts;
67
using Utilities.Validators;
@@ -39,9 +40,5 @@ private set
3940
this.testContext = value;
4041
}
4142
}
42-
43-
public Action<ComponentTestContext> ComponentPreparationAction { get; set; }
44-
45-
public Action<ComponentTestContext> PreInvocationAction { get; set; }
4643
}
4744
}

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestContexts/ComponentTestContext.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
public abstract class ComponentTestContext : HttpTestContext
1212
{
1313
private object component;
14-
private IEnumerable<object> controllerAttributes;
14+
private IEnumerable<object> componentAttributes;
15+
private IDictionary<Type, object> aggregatedServices;
1516
private string methodName;
1617
private MethodInfo method;
1718
private LambdaExpression methodCall;
@@ -35,12 +36,25 @@ public IEnumerable<object> ComponentAttributes
3536
{
3637
get
3738
{
38-
if (this.controllerAttributes == null)
39+
if (this.componentAttributes == null)
3940
{
40-
this.controllerAttributes = Reflection.GetCustomAttributes(this.Component);
41+
this.componentAttributes = Reflection.GetCustomAttributes(this.Component);
4142
}
4243

43-
return this.controllerAttributes;
44+
return this.componentAttributes;
45+
}
46+
}
47+
48+
public IDictionary<Type, object> AggregatedServices
49+
{
50+
get
51+
{
52+
if (this.aggregatedServices == null)
53+
{
54+
this.aggregatedServices = new Dictionary<Type, object>();
55+
}
56+
57+
return this.aggregatedServices;
4458
}
4559
}
4660

@@ -119,8 +133,12 @@ public object Model
119133
this.model = value;
120134
}
121135
}
122-
136+
123137
public Func<object> ComponentConstruction { get; set; }
138+
139+
public Action ComponentPreparationAction { get; set; }
140+
141+
public Action PreMethodInvocationAction { get; set; }
124142

125143
public TComponent ComponentAs<TComponent>()
126144
where TComponent : class => this.Component as TComponent;

src/MyTested.AspNetCore.Mvc.Core/Builders/Contracts/Controllers/IAndControllerBuilder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Controllers
22
{
3-
using Base;
4-
53
/// <summary>
64
/// Used for adding AndAlso() method to the controller builder.
75
/// </summary>

src/MyTested.AspNetCore.Mvc.Core/Builders/Contracts/Controllers/IControllerBuilder.cs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Controllers
22
{
33
using System;
4-
using System.Collections.Generic;
54
using System.Linq.Expressions;
65
using System.Threading.Tasks;
76
using Actions;
87
using Base;
9-
using Microsoft.AspNetCore.Http;
108
using Microsoft.AspNetCore.Mvc;
119
using ShouldPassFor;
1210

@@ -20,7 +18,7 @@ public interface IControllerBuilder<TController> : IBaseTestBuilderWithComponent
2018
/// <summary>
2119
/// Used for testing controller additional data.
2220
/// </summary>
23-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
21+
/// <returns>Test builder of <see cref="IControllerTestBuilder"/> type.</returns>
2422
IControllerTestBuilder ShouldHave();
2523

2624
/// <summary>
@@ -51,52 +49,6 @@ public interface IControllerBuilder<TController> : IBaseTestBuilderWithComponent
5149
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
5250
IAndControllerBuilder<TController> WithActionContext(Action<ActionContext> actionContextSetup);
5351

54-
/// <summary>
55-
/// Configures a service with scoped lifetime by using the provided <see cref="Action"/> delegate.
56-
/// </summary>
57-
/// <typeparam name="TService">Type of service to configure.</typeparam>
58-
/// <param name="scopedServiceSetup">Action configuring the service before running the test case.</param>
59-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
60-
IAndControllerBuilder<TController> WithServiceSetupFor<TService>(Action<TService> scopedServiceSetup)
61-
where TService : class;
62-
63-
/// <summary>
64-
/// Sets null value to the constructor service dependency of the given type.
65-
/// </summary>
66-
/// <typeparam name="TService">Type of service dependency.</typeparam>
67-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
68-
IAndControllerBuilder<TController> WithNoServiceFor<TService>()
69-
where TService : class;
70-
71-
/// <summary>
72-
/// Tries to resolve constructor service dependency of the given type.
73-
/// </summary>
74-
/// <typeparam name="TService">Type of service dependency to resolve.</typeparam>
75-
/// <param name="service">Instance of service dependency to inject into the controller constructor.</param>
76-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
77-
IAndControllerBuilder<TController> WithServiceFor<TService>(TService service)
78-
where TService : class;
79-
80-
/// <summary>
81-
/// Tries to resolve constructor service dependencies by the provided collection of objects.
82-
/// </summary>
83-
/// <param name="services">Collection of service dependencies to inject into the controller constructor.</param>
84-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
85-
IAndControllerBuilder<TController> WithServices(IEnumerable<object> services);
86-
87-
/// <summary>
88-
/// Tries to resolve constructor service dependencies by the provided parameters.
89-
/// </summary>
90-
/// <param name="services">Services to inject into the controller constructor.</param>
91-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
92-
IAndControllerBuilder<TController> WithServices(params object[] services);
93-
94-
/// <summary>
95-
/// Disables <see cref="Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary"/> validation for the action call.
96-
/// </summary>
97-
/// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
98-
IAndControllerBuilder<TController> WithoutValidation();
99-
10052
/// <summary>
10153
/// Sets custom properties to the controller using a delegate.
10254
/// </summary>

src/MyTested.AspNetCore.Mvc.Core/Builders/Controllers/ControllerActionCallBuilder.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Threading.Tasks;
88
using Actions;
99
using Contracts.Actions;
10-
using Contracts.Controllers;
1110
using Internal.Contracts;
1211
using Internal.TestContexts;
1312
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
@@ -21,13 +20,6 @@
2120
/// </content>
2221
public partial class ControllerBuilder<TController>
2322
{
24-
/// <inheritdoc />
25-
public IAndControllerBuilder<TController> WithoutValidation()
26-
{
27-
this.enabledValidation = false;
28-
return this;
29-
}
30-
3123
/// <inheritdoc />
3224
public IActionResultTestBuilder<TActionResult> Calling<TActionResult>(Expression<Func<TController, TActionResult>> actionCall)
3325
{
@@ -123,11 +115,11 @@ private ActionTestContext<TActionResult> GetAndValidateActionResult<TActionResul
123115
private string GetAndValidateAction(LambdaExpression actionCall)
124116
{
125117
this.BuildControllerIfNotExists();
126-
this.PreInvocationAction?.Invoke(this.TestContext);
118+
this.TestContext.PreMethodInvocationAction?.Invoke();
127119

128120
var methodInfo = ExpressionParser.GetMethodInfo(actionCall);
129121

130-
if (this.enabledValidation)
122+
if (this.EnabledValidation)
131123
{
132124
this.ValidateModelState(actionCall);
133125
}

0 commit comments

Comments
 (0)