-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better support for testing internal types (#146)
* Abstracted test logic into TestBase class * Added types to better support testing internal scoped types
- Loading branch information
1 parent
a75e016
commit 431b378
Showing
30 changed files
with
919 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
Neovolve.Streamline.NSubstitute.UnitTests/TestsInternalTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
namespace Neovolve.Streamline.NSubstitute.UnitTests; | ||
|
||
using System; | ||
using FluentAssertions; | ||
using global::NSubstitute; | ||
using Xunit; | ||
|
||
public class TestsInternalTests | ||
{ | ||
[Fact] | ||
public void CanCreateMissingServices() | ||
{ | ||
var id = Guid.NewGuid(); | ||
var expected = Guid.NewGuid().ToString(); | ||
|
||
var wrapper = new TestsInternalWrapper<TypeWithDependency>(); | ||
|
||
wrapper.Service<ITargetService>().GetValue(id).Returns(expected); | ||
|
||
var actual = wrapper.SUT.GetValue(id); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void CanCreateSUTWithVirtualMembers() | ||
{ | ||
// In this test we are proving that using Tests<T> returns an instance of T rather than a substitute of T | ||
var expected = Guid.NewGuid().ToString(); | ||
var id = Guid.NewGuid(); | ||
|
||
var wrapper = new TestsInternalWrapper<TargetWithVirtual>(); | ||
|
||
wrapper.Service<ITargetService>().GetValue(id).Returns(expected); | ||
|
||
wrapper.SUT.Should().NotBeNull(); | ||
wrapper.SUT.Service.Should().NotBeNull(); | ||
|
||
var actual = wrapper.SUT.RunTest(id); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void CanCreateWithConstructorProvidedService() | ||
{ | ||
var id = Guid.NewGuid(); | ||
var expected = Guid.NewGuid().ToString(); | ||
|
||
var service = Substitute.For<ITargetService>(); | ||
|
||
service.GetValue(id).Returns(expected); | ||
|
||
var wrapper = new TestsInternalWrapper<TypeWithDependency>(service); | ||
|
||
var actual = wrapper.SUT.GetValue(id); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
private class TargetWithVirtual | ||
{ | ||
// ReSharper disable once UnusedMember.Global | ||
// ReSharper disable once UnusedMember.Local | ||
public TargetWithVirtual(ITargetService service) | ||
{ | ||
Service = service; | ||
} | ||
|
||
public virtual string RunTest(Guid id) | ||
{ | ||
var service = Service ?? throw new InvalidOperationException("No service defined"); | ||
|
||
return service.GetValue(id); | ||
} | ||
|
||
public ITargetService? Service { get; } | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Neovolve.Streamline.NSubstitute.UnitTests/TestsInternalWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Neovolve.Streamline.NSubstitute.UnitTests; | ||
|
||
using global::NSubstitute; | ||
|
||
internal class TestsInternalWrapper<T> : TestsInternal where T : class | ||
{ | ||
public TestsInternalWrapper(params object[] values) : base(values) | ||
{ | ||
} | ||
|
||
public T SUT => GetSUT<T>(); | ||
} |
24 changes: 24 additions & 0 deletions
24
Neovolve.Streamline.NSubstitute.UnitTests/TestsPartOfInternalTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Neovolve.Streamline.NSubstitute.UnitTests; | ||
|
||
using System; | ||
using FluentAssertions; | ||
using global::NSubstitute; | ||
using Xunit; | ||
|
||
public class TestsPartOfInternalTests | ||
{ | ||
[Fact] | ||
public void CanPartialMockSUT() | ||
{ | ||
var id = Guid.NewGuid(); | ||
var expected = Guid.NewGuid().ToString(); | ||
|
||
var wrapper = new TestsPartOfInternalWrapper<TypeWithVirtual>(); | ||
|
||
wrapper.SUT.GetValueEx(id).Returns(expected); | ||
|
||
var actual = wrapper.SUT.GetValue(id); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Neovolve.Streamline.NSubstitute.UnitTests/TestsPartOfInternalWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Neovolve.Streamline.NSubstitute.UnitTests; | ||
|
||
using global::NSubstitute; | ||
|
||
internal class TestsPartOfInternalWrapper<T> : TestsPartOfInternal where T : class | ||
{ | ||
public TestsPartOfInternalWrapper(params object[] values) : base(values) | ||
{ | ||
} | ||
|
||
public T SUT => GetSUT<T>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Neovolve.Streamline.NSubstitute.UnitTests/TestsSubstituteOfInternalTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
namespace Neovolve.Streamline.NSubstitute.UnitTests; | ||
|
||
using System; | ||
using FluentAssertions; | ||
using global::NSubstitute; | ||
using Xunit; | ||
|
||
public class TestsSubstituteOfInternalTests | ||
{ | ||
[Fact] | ||
public void CanSubstituteAbstractTypeWithoutParameters() | ||
{ | ||
var id = Guid.NewGuid(); | ||
var expected = Guid.NewGuid().ToString(); | ||
|
||
var wrapper = new TestsSubstituteOfInternalWrapper<AbstractType>(); | ||
|
||
wrapper.SUT.GetCustomValue(id).Returns(expected); | ||
|
||
var actual = wrapper.SUT.GetValue(id); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void CanSubstituteAbstractTypeWithParameters() | ||
{ | ||
var id = Guid.NewGuid(); | ||
var expected = Guid.NewGuid().ToString(); | ||
|
||
var wrapper = new TestsSubstituteOfInternalWrapper<AbstractTypeWithDependency>(); | ||
|
||
wrapper.SUT.GetValueFromService(id).Returns(expected); | ||
|
||
var actual = wrapper.SUT.GetValue(id); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void CanSubstituteTypeWithDefaultConstructor() | ||
{ | ||
var id = Guid.NewGuid(); | ||
var expected = Guid.NewGuid().ToString(); | ||
|
||
var wrapper = new TestsSubstituteOfInternalWrapper<TypeWithDefaultConstructor>(); | ||
|
||
wrapper.SUT.GetCustomValue(id).Returns(expected); | ||
|
||
var actual = wrapper.SUT.GetValue(id); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void CanSubstituteVirtualMethod() | ||
{ | ||
var id = Guid.NewGuid(); | ||
var expected = Guid.NewGuid().ToString(); | ||
|
||
var wrapper = new TestsSubstituteOfInternalWrapper<TypeWithDependency>(); | ||
|
||
wrapper.SUT.GetValueFromService(id).Returns(expected); | ||
|
||
var actual = wrapper.SUT.GetValue(id); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Neovolve.Streamline.NSubstitute.UnitTests/TestsSubstituteOfInternalWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Neovolve.Streamline.NSubstitute.UnitTests; | ||
|
||
using global::NSubstitute; | ||
|
||
internal class TestsSubstituteOfInternalWrapper<T> : TestsSubstituteOfInternal where T : class | ||
{ | ||
public TestsSubstituteOfInternalWrapper(params object[] values) : base(values) | ||
{ | ||
} | ||
|
||
public T SUT => GetSUT<T>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.