Skip to content

Commit 9390cb7

Browse files
committed
Added tests for WithDbContext (#125)
1 parent 7b16ae7 commit 9390cb7

File tree

6 files changed

+91
-37
lines changed

6 files changed

+91
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace MyApp.Tests
4545
{
4646
base.ConfigureServices(services);
4747

48-
services.Replace<IRepository, MockedRepository>();
48+
services.Replace<IService, MockedService>();
4949
}
5050
}
5151
}

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/ControllerBuilderEntityFrameworkCoreExtensions.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,6 @@
1111
/// </summary>
1212
public static class ControllerBuilderEntityFrameworkCoreExtensions
1313
{
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-
3714
/// <summary>
3815
/// Sets initial values to the <see cref="DbContext"/> on the tested controller.
3916
/// </summary>

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/MyTested.AspNetCore.Mvc.EntityFrameworkCore.xproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616
<SchemaVersion>2.0</SchemaVersion>
1717
</PropertyGroup>
1818
<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>
1922
</Project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
3+
using Xunit;
44

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("")]
105
[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.
166
[assembly: ComVisible(false)]
177

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)]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)