File tree Expand file tree Collapse file tree 6 files changed +91
-37
lines changed
src/MyTested.AspNetCore.Mvc.EntityFrameworkCore
test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test
BuildersTests/ControllerTests Expand file tree Collapse file tree 6 files changed +91
-37
lines changed Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ namespace MyApp.Tests
45
45
{
46
46
base .ConfigureServices (services );
47
47
48
- services .Replace <IRepository , MockedRepository >();
48
+ services .Replace <IService , MockedService >();
49
49
}
50
50
}
51
51
}
Original file line number Diff line number Diff line change 11
11
/// </summary>
12
12
public static class ControllerBuilderEntityFrameworkCoreExtensions
13
13
{
14
- /// <summary>
15
- /// Sets initial values to the <see cref="DbContext"/> on the tested controller.
16
- /// </summary>
17
- /// <typeparam name="TController">Class representing ASP.NET Core MVC controller.</typeparam>
18
- /// <param name="controllerBuilder">Instance of <see cref="IControllerBuilder{TController}"/> type.</param>
19
- /// <param name="dbContextSetup">Action setting the <see cref="DbContext"/>.</param>
20
- /// <returns>The same <see cref="IControllerBuilder{TController}"/>.</returns>
21
- public static IControllerBuilder < TController > WithDbContext < TController > (
22
- this IControllerBuilder < TController > controllerBuilder ,
23
- Action < DbContext > dbContextSetup )
24
- where TController : class
25
- {
26
- var actualControllerBuilder = ( ControllerBuilder < TController > ) controllerBuilder ;
27
-
28
- actualControllerBuilder . WithServiceSetupFor < DbContext > ( dbContext =>
29
- {
30
- dbContextSetup ( dbContext ) ;
31
- dbContext . SaveChanges ( ) ;
32
- } ) ;
33
-
34
- return actualControllerBuilder ;
35
- }
36
-
37
14
/// <summary>
38
15
/// Sets initial values to the <see cref="DbContext"/> on the tested controller.
39
16
/// </summary>
Original file line number Diff line number Diff line change 16
16
<SchemaVersion >2.0</SchemaVersion >
17
17
</PropertyGroup >
18
18
<Import Project =" $(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition =" '$(VSToolsPath)' != ''" />
19
+ <Target Name =" AfterBuild" >
20
+ <Exec Condition =" '$(Configuration)|$(Platform)'=='Release|AnyCPU'" Command =" dotnet pack --configuration Release --output ../../artifacts --no-build" />
21
+ </Target >
19
22
</Project >
Original file line number Diff line number Diff line change
1
+ namespace MyTested . AspNetCore . Mvc . EntityFrameworkCore . Test . BuildersTests . ControllerTests
2
+ {
3
+ using Microsoft . EntityFrameworkCore ;
4
+ using Microsoft . Extensions . DependencyInjection ;
5
+ using Setups ;
6
+ using Setups . Common ;
7
+ using Xunit ;
8
+
9
+ public class ControllerBuilderTests
10
+ {
11
+ [ Fact ]
12
+ public void WithDbContextShouldSetupDbContext ( )
13
+ {
14
+ MyMvc
15
+ . IsUsingDefaultConfiguration ( )
16
+ . WithServices ( services =>
17
+ {
18
+ services . AddDbContext < CustomDbContext > ( options =>
19
+ options . UseSqlServer ( "Server=(localdb)\\ MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;" ) ) ;
20
+ } ) ;
21
+
22
+ MyMvc
23
+ . Controller < DbContextController > ( )
24
+ . WithDbContext ( dbContext => dbContext
25
+ . WithSetup < CustomDbContext > ( db => db
26
+ . Models . Add ( new CustomModel
27
+ {
28
+ Id = 1 , Name = "Test"
29
+ } ) ) )
30
+ . Calling ( c => c . Find ( 1 ) )
31
+ . ShouldReturn ( )
32
+ . Ok ( )
33
+ . WithResponseModelOfType < CustomModel > ( )
34
+ . Passing ( m => m . Name == "Test" ) ;
35
+
36
+ MyMvc
37
+ . Controller < DbContextController > ( )
38
+ . WithDbContext ( dbContext => dbContext
39
+ . WithSetup < CustomDbContext > ( db => db
40
+ . Models . Add ( new CustomModel
41
+ {
42
+ Id = 2 ,
43
+ Name = "Test"
44
+ } ) ) )
45
+ . Calling ( c => c . Find ( 1 ) )
46
+ . ShouldReturn ( )
47
+ . NotFound ( ) ;
48
+
49
+ MyMvc
50
+ . Controller < DbContextController > ( )
51
+ . Calling ( c => c . Find ( 1 ) )
52
+ . ShouldReturn ( )
53
+ . NotFound ( ) ;
54
+
55
+ MyMvc . IsUsingDefaultConfiguration ( ) ;
56
+ }
57
+ }
58
+ }
Original file line number Diff line number Diff line change 1
1
using System . Reflection ;
2
- using System . Runtime . CompilerServices ;
3
2
using System . Runtime . InteropServices ;
3
+ using Xunit ;
4
4
5
- // General Information about an assembly is controlled through the following
6
- // set of attributes. Change these attribute values to modify the information
7
- // associated with an assembly.
8
- [ assembly: AssemblyConfiguration ( "" ) ]
9
- [ assembly: AssemblyCompany ( "" ) ]
10
5
[ assembly: AssemblyProduct ( "MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test" ) ]
11
- [ assembly: AssemblyTrademark ( "" ) ]
12
-
13
- // Setting ComVisible to false makes the types in this assembly not visible
14
- // to COM components. If you need to access a type in this assembly from
15
- // COM, set the ComVisible attribute to true on that type.
16
6
[ assembly: ComVisible ( false ) ]
17
7
18
- // The following GUID is for the ID of the typelib if this project is exposed to COM
19
- [ assembly: Guid ( "285e08f2-2e5a-4226-b867-fd3df7c52053" ) ]
8
+ [ assembly: CollectionBehavior ( DisableTestParallelization = true ) ]
Original file line number Diff line number Diff line change
1
+ namespace MyTested . AspNetCore . Mvc . EntityFrameworkCore . Test . Setups
2
+ {
3
+ using System . Linq ;
4
+ using Common ;
5
+ using Microsoft . AspNetCore . Mvc ;
6
+
7
+ public class DbContextController : Controller
8
+ {
9
+ private readonly CustomDbContext data ;
10
+
11
+ public DbContextController ( CustomDbContext data )
12
+ {
13
+ this . data = data ;
14
+ }
15
+
16
+ public IActionResult Find ( int id )
17
+ {
18
+ var model = this . data . Models . FirstOrDefault ( m => m . Id == id ) ;
19
+ if ( model == null )
20
+ {
21
+ return this . NotFound ( ) ;
22
+ }
23
+
24
+ return this . Ok ( model ) ;
25
+ }
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments