Skip to content

Commit 7846def

Browse files
author
Jennifer Deigendesch
committed
Added unit tests
1 parent 8364a44 commit 7846def

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

ASPNETCoreExample.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASPNETCoreExample", "src\AS
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataDAL", "DataDAL\DataDAL.csproj", "{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{4A7375F3-CF33-419D-ABC7-F6F7EA02D0DE}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -25,12 +27,17 @@ Global
2527
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
2628
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
2729
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{4A7375F3-CF33-419D-ABC7-F6F7EA02D0DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{4A7375F3-CF33-419D-ABC7-F6F7EA02D0DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{4A7375F3-CF33-419D-ABC7-F6F7EA02D0DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{4A7375F3-CF33-419D-ABC7-F6F7EA02D0DE}.Release|Any CPU.Build.0 = Release|Any CPU
2834
EndGlobalSection
2935
GlobalSection(SolutionProperties) = preSolution
3036
HideSolutionNode = FALSE
3137
EndGlobalSection
3238
GlobalSection(NestedProjects) = preSolution
3339
{56CF75C7-C267-417C-BCF6-69CEBC0BF27C} = {F7B9D11A-69C3-4AAA-B3B4-DC19B0B53D1D}
3440
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E} = {F7B9D11A-69C3-4AAA-B3B4-DC19B0B53D1D}
41+
{4A7375F3-CF33-419D-ABC7-F6F7EA02D0DE} = {F7B9D11A-69C3-4AAA-B3B4-DC19B0B53D1D}
3542
EndGlobalSection
3643
EndGlobal

Tests/RepositoryTests/TestBase.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace Tests.RepositoryTests
2+
{
3+
using System;
4+
5+
using DataDAL;
6+
7+
using Microsoft.Data.Sqlite;
8+
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
11+
public class TestBase
12+
{
13+
private SqliteConnection connection;
14+
15+
protected DataDAL Context { get; private set; }
16+
17+
[TestCleanup]
18+
19+
// ReSharper disable once UnusedMember.Global, because it's a TestCleanup-method, which must be public.
20+
public virtual void CleanUp()
21+
{
22+
this.connection.Close();
23+
}
24+
25+
[TestInitialize]
26+
27+
// ReSharper disable once MemberCanBeProtected.Global, because TestInitialize-methods must be public.
28+
public virtual void SetUp()
29+
{
30+
this.connection = CreateConnection();
31+
this.connection.Open();
32+
33+
this.Context = EnsureDatabaseCreated(this.connection);
34+
}
35+
36+
public static SqliteConnection CreateConnection()
37+
{
38+
// In-memory database only exists while the connection is open
39+
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" };
40+
return new SqliteConnection(connectionStringBuilder.ToString());
41+
}
42+
43+
public static DataDAL EnsureDatabaseCreated(SqliteConnection connection)
44+
{
45+
var contextOptions = new DbContextOptionsBuilder<DataDAL>().UseSqlite(connection)
46+
.Options;
47+
48+
// Create the schema in the database if not available
49+
var context = (DataDAL)Activator.CreateInstance(typeof(DataDAL), contextOptions);
50+
context.Database.OpenConnection();
51+
context.Database.EnsureCreated();
52+
53+
return context;
54+
}
55+
}
56+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Tests.RepositoryTests.UserRepositoryTests
2+
{
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
[TestClass]
9+
public class GetUserTests : UserRepositoryTestsBase
10+
{
11+
[TestMethod]
12+
public async Task ShouldReturnSuccessfulWhenUserWithIdAvailable()
13+
{
14+
// Arrange
15+
const string Id = "1";
16+
const string Name = "Jennifer";
17+
await UserRepositoryUtils.AddUserAsync(this.Context, Id, Name);
18+
19+
// Test
20+
var result = await this.RepositoryUnderTest.GetUserAsync(Id);
21+
22+
// Assert
23+
Assert.IsNotNull(result);
24+
Assert.AreEqual(Id, result.Id);
25+
Assert.AreEqual(Name, result.Name);
26+
}
27+
28+
[TestMethod]
29+
[ExpectedException(typeof(InvalidOperationException))]
30+
public async Task ShouldReturnErrorWhenUserWithIdMissing()
31+
{
32+
// Arrange
33+
const string Id = "1";
34+
35+
// Test
36+
await this.RepositoryUnderTest.GetUserAsync(Id);
37+
}
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Tests.RepositoryTests.UserRepositoryTests
2+
{
3+
using AspNetCoreExample.Repositories;
4+
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
public class UserRepositoryTestsBase : TestBase
8+
{
9+
protected UserRepository RepositoryUnderTest { get; private set; }
10+
11+
[TestInitialize]
12+
public override void SetUp()
13+
{
14+
base.SetUp();
15+
this.RepositoryUnderTest = new UserRepository(this.Context);
16+
}
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Tests.RepositoryTests.UserRepositoryTests
2+
{
3+
using System.Threading.Tasks;
4+
5+
using DataDAL;
6+
using DataDAL.Tables;
7+
8+
public static class UserRepositoryUtils
9+
{
10+
public static async Task AddUserAsync(DataDAL context, string id, string name)
11+
{
12+
context.User.Add(new User { Id = id, Name = name });
13+
await context.SaveChangesAsync();
14+
}
15+
}
16+
}

Tests/Tests.csproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Data.SQLite" Version="1.1.0" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.1" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
11+
<PackageReference Include="Moq" Version="4.7.9" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="1.1.11" />
13+
<PackageReference Include="MSTest.TestFramework" Version="1.1.11" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\src\ASPNETCoreExample\ASPNETCoreExample.csproj" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
22+
</ItemGroup>
23+
24+
</Project>

0 commit comments

Comments
 (0)