diff --git a/pages/ef-mstest-usage.md b/pages/ef-mstest-usage.md index d63d4427..24732c56 100644 --- a/pages/ef-mstest-usage.md +++ b/pages/ef-mstest-usage.md @@ -767,7 +767,7 @@ Mark a test method with `[NoDb]` when it does not touch the database, for exampl A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked. -In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws. +In a `[NoDb]` test `Database` and `ArrangeData` are null, so code under test that takes a context it never uses can still be constructed with `ArrangeData`. Accessing `ActData` or `AssertData`, or calling `Reset`, throws. In a database test, recording resumes on the first access of `ActData`. A `[NoDb]` test has no `ActData`, so recording is resumed when the test starts. Other recorded activity, such as NServiceBus messages sent by the code under test, still appears in the snapshot. diff --git a/pages/ef-nunit-usage.md b/pages/ef-nunit-usage.md index ab0d8209..c3df5082 100644 --- a/pages/ef-nunit-usage.md +++ b/pages/ef-nunit-usage.md @@ -778,7 +778,7 @@ Mark a test method with `[NoDb]` when it does not touch the database, for exampl A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked. -In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws. +In a `[NoDb]` test `Database` and `ArrangeData` are null, so code under test that takes a context it never uses can still be constructed with `ArrangeData`. Accessing `ActData` or `AssertData`, or calling `Reset`, throws. In a database test, recording resumes on the first access of `ActData`. A `[NoDb]` test has no `ActData`, so recording is resumed when the test starts. Other recorded activity, such as NServiceBus messages sent by the code under test, still appears in the snapshot. diff --git a/pages/ef-tunit-usage.md b/pages/ef-tunit-usage.md index 6a648593..2aa955bc 100644 --- a/pages/ef-tunit-usage.md +++ b/pages/ef-tunit-usage.md @@ -774,7 +774,7 @@ Mark a test method with `[NoDb]` when it does not touch the database, for exampl A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked. -In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws. +In a `[NoDb]` test `Database` and `ArrangeData` are null, so code under test that takes a context it never uses can still be constructed with `ArrangeData`. Accessing `ActData` or `AssertData`, or calling `Reset`, throws. In a database test, recording resumes on the first access of `ActData`. A `[NoDb]` test has no `ActData`, so recording is resumed when the test starts. Other recorded activity, such as NServiceBus messages sent by the code under test, still appears in the snapshot. diff --git a/pages/ef-xunitv3-usage.md b/pages/ef-xunitv3-usage.md index 9ff960ad..1019bda7 100644 --- a/pages/ef-xunitv3-usage.md +++ b/pages/ef-xunitv3-usage.md @@ -772,7 +772,7 @@ Mark a test method with `[NoDb]` when it does not touch the database, for exampl A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked. -In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws. +In a `[NoDb]` test `Database` and `ArrangeData` are null, so code under test that takes a context it never uses can still be constructed with `ArrangeData`. Accessing `ActData` or `AssertData`, or calling `Reset`, throws. In a database test, recording resumes on the first access of `ActData`. A `[NoDb]` test has no `ActData`, so recording is resumed when the test starts. Other recorded activity, such as NServiceBus messages sent by the code under test, still appears in the snapshot. diff --git a/pages/mdsource/no-db.include.md b/pages/mdsource/no-db.include.md index 9c7999fc..1384bf42 100644 --- a/pages/mdsource/no-db.include.md +++ b/pages/mdsource/no-db.include.md @@ -2,7 +2,7 @@ Mark a test method with `[NoDb]` when it does not touch the database, for exampl A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked. -In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws. +In a `[NoDb]` test `Database` and `ArrangeData` are null, so code under test that takes a context it never uses can still be constructed with `ArrangeData`. Accessing `ActData` or `AssertData`, or calling `Reset`, throws. In a database test, recording resumes on the first access of `ActData`. A `[NoDb]` test has no `ActData`, so recording is resumed when the test starts. Other recorded activity, such as NServiceBus messages sent by the code under test, still appears in the snapshot. diff --git a/src/EfLocalDb.MSTest.Tests/NoDbTests.cs b/src/EfLocalDb.MSTest.Tests/NoDbTests.cs index 6a49b3d2..072656a8 100644 --- a/src/EfLocalDb.MSTest.Tests/NoDbTests.cs +++ b/src/EfLocalDb.MSTest.Tests/NoDbTests.cs @@ -8,9 +8,14 @@ public void HasNoDatabase() => [TestMethod] [NoDb] - public void ArrangeDataThrows() + public void ArrangeDataIsNull() => + Assert.IsNull(ArrangeData); + + [TestMethod] + [NoDb] + public void ActDataThrows() { - var exception = Assert.ThrowsExactly(() => _ = ArrangeData); + var exception = Assert.ThrowsExactly(() => _ = ActData); Assert.Contains("[NoDb]", exception.Message); } diff --git a/src/EfLocalDb.MSTest/LocalDbTestBase.cs b/src/EfLocalDb.MSTest/LocalDbTestBase.cs index eb97ffc1..71e602d0 100644 --- a/src/EfLocalDb.MSTest/LocalDbTestBase.cs +++ b/src/EfLocalDb.MSTest/LocalDbTestBase.cs @@ -154,7 +154,7 @@ void ThrowIfNoDb() { if (isNoDb) { - throw new("The test is marked [NoDb], so it has no database. Remove [NoDb] to use ArrangeData, ActData, AssertData, or Reset."); + throw new("The test is marked [NoDb], so it has no database. Remove [NoDb] to use ActData, AssertData, or Reset."); } } @@ -162,7 +162,13 @@ public virtual T ArrangeData { get { - ThrowIfNoDb(); + // Code under test often takes a context it never uses in a [NoDb] test, + // so return null rather than throw + if (isNoDb) + { + return null!; + } + if (phase == Phase.Act) { throw new("Phase has already moved to Act. Check for a ActData usage in the preceding code."); diff --git a/src/EfLocalDb.NUnit.Tests/NoDbTests.cs b/src/EfLocalDb.NUnit.Tests/NoDbTests.cs index a6dfe929..f9ac9305 100644 --- a/src/EfLocalDb.NUnit.Tests/NoDbTests.cs +++ b/src/EfLocalDb.NUnit.Tests/NoDbTests.cs @@ -9,9 +9,14 @@ public void HasNoDatabase() => [Test] [NoDb] - public void ArrangeDataThrows() + public void ArrangeDataIsNull() => + IsNull(ArrangeData); + + [Test] + [NoDb] + public void ActDataThrows() { - var exception = Throws(() => _ = ArrangeData)!; + var exception = Throws(() => _ = ActData)!; That(exception.Message, Does.Contain("[NoDb]")); } diff --git a/src/EfLocalDb.NUnit/LocalDbTestBase.cs b/src/EfLocalDb.NUnit/LocalDbTestBase.cs index 5a0f03cc..6c2536d7 100644 --- a/src/EfLocalDb.NUnit/LocalDbTestBase.cs +++ b/src/EfLocalDb.NUnit/LocalDbTestBase.cs @@ -156,7 +156,7 @@ void ThrowIfNoDb() { if (isNoDb) { - throw new("The test is marked [NoDb], so it has no database. Remove [NoDb] to use ArrangeData, ActData, AssertData, or Reset."); + throw new("The test is marked [NoDb], so it has no database. Remove [NoDb] to use ActData, AssertData, or Reset."); } } @@ -164,7 +164,13 @@ public virtual T ArrangeData { get { - ThrowIfNoDb(); + // Code under test often takes a context it never uses in a [NoDb] test, + // so return null rather than throw + if (isNoDb) + { + return null!; + } + if (phase == Phase.Act) { throw new("Phase has already moved to Act. Check for a ActData usage in the preceding code."); diff --git a/src/EfLocalDb.TUnit.Tests/NoDbTests.cs b/src/EfLocalDb.TUnit.Tests/NoDbTests.cs index bd2a8752..3bd61c94 100644 --- a/src/EfLocalDb.TUnit.Tests/NoDbTests.cs +++ b/src/EfLocalDb.TUnit.Tests/NoDbTests.cs @@ -7,9 +7,14 @@ public async Task HasNoDatabase() => [Test] [NoDb] - public async Task ArrangeDataThrows() + public async Task ArrangeDataIsNull() => + await Assert.That(ArrangeData).IsNull(); + + [Test] + [NoDb] + public async Task ActDataThrows() { - var exception = await Assert.That(() => _ = ArrangeData).Throws(); + var exception = await Assert.That(() => _ = ActData).Throws(); await Assert.That(exception!.Message).Contains("[NoDb]"); } diff --git a/src/EfLocalDb.TUnit/LocalDbTestBase.cs b/src/EfLocalDb.TUnit/LocalDbTestBase.cs index 2983b32b..14a0176e 100644 --- a/src/EfLocalDb.TUnit/LocalDbTestBase.cs +++ b/src/EfLocalDb.TUnit/LocalDbTestBase.cs @@ -163,7 +163,7 @@ void ThrowIfNoDb() { if (isNoDb) { - throw new("The test is marked [NoDb], so it has no database. Remove [NoDb] to use ArrangeData, ActData, AssertData, or Reset."); + throw new("The test is marked [NoDb], so it has no database. Remove [NoDb] to use ActData, AssertData, or Reset."); } } @@ -171,7 +171,13 @@ public virtual T ArrangeData { get { - ThrowIfNoDb(); + // Code under test often takes a context it never uses in a [NoDb] test, + // so return null rather than throw + if (isNoDb) + { + return null!; + } + if (phase == Phase.Act) { throw new("Phase has already moved to Act. Check for a ActData usage in the preceding code."); diff --git a/src/EfLocalDb.Xunit.V3.Tests/NoDbTests.cs b/src/EfLocalDb.Xunit.V3.Tests/NoDbTests.cs index 6f2dba23..f2036bf1 100644 --- a/src/EfLocalDb.Xunit.V3.Tests/NoDbTests.cs +++ b/src/EfLocalDb.Xunit.V3.Tests/NoDbTests.cs @@ -7,9 +7,14 @@ public void HasNoDatabase() => [Fact] [NoDb] - public void ArrangeDataThrows() + public void ArrangeDataIsNull() => + Assert.Null(ArrangeData); + + [Fact] + [NoDb] + public void ActDataThrows() { - var exception = Assert.Throws(() => _ = ArrangeData); + var exception = Assert.Throws(() => _ = ActData); Assert.Contains("[NoDb]", exception.Message); } diff --git a/src/EfLocalDb.Xunit.V3/LocalDbTestBase.cs b/src/EfLocalDb.Xunit.V3/LocalDbTestBase.cs index 5de2d8d0..27bf5468 100644 --- a/src/EfLocalDb.Xunit.V3/LocalDbTestBase.cs +++ b/src/EfLocalDb.Xunit.V3/LocalDbTestBase.cs @@ -158,7 +158,7 @@ void ThrowIfNoDb() { if (isNoDb) { - throw new("The test is marked [NoDb], so it has no database. Remove [NoDb] to use ArrangeData, ActData, AssertData, or Reset."); + throw new("The test is marked [NoDb], so it has no database. Remove [NoDb] to use ActData, AssertData, or Reset."); } } @@ -166,7 +166,13 @@ public virtual T ArrangeData { get { - ThrowIfNoDb(); + // Code under test often takes a context it never uses in a [NoDb] test, + // so return null rather than throw + if (isNoDb) + { + return null!; + } + if (phase == Phase.Act) { throw new("Phase has already moved to Act. Check for a ActData usage in the preceding code.");