-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathTestBase.cs
32 lines (30 loc) · 1.16 KB
/
TestBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using NUnit.Framework;
using System.Threading.Tasks;
namespace MyWarehouse.Application.IntegrationTests
{
/// <summary>
/// Derive all test classes from this base class to implement the cross-cutting concern
/// of creating/disposing a context and resetting the database after each test run.
/// </summary>
public abstract class TestBase
{
[SetUp]
public void BeforeTest()
{
// This creates a new DbContext for the duration of the test.
// Having a per-test DbContext instead of a per-operation one
// helps tests which touch multiple entities.
// It allows them to retrieve entities and add them to other entities,
// because the retrieved entities are still tracked by the change tracker.
// Without a per-test DbContext we'd need to attach all such entities
// when saving as part of other entities.
TestFramework.Context.CreateScope();
}
[TearDown]
public async Task AfterTest()
{
await TestFramework.Data.ResetDatabase();
TestFramework.Context.DisposeScope();
}
}
}