Skip to content

Commit 5e845d7

Browse files
author
Robert Lindblom
committed
Updated to target relevant frameworks.
2 parents d595dd7 + d929f68 commit 5e845d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+441
-1269
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,5 @@ pip-log.txt
161161

162162
# Mac crap
163163
.DS_Store
164+
165+
**/.vs

AutoMockerNSubstitute.1.0.0.nupkg

11.4 KB
Binary file not shown.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
This repo was forked from [NSubstituteAutoMocker](https://github.com/kevin-mcmanus/NSubstituteAutoMocker), which seems quite dead, and is updated to be able to target relevant frameworks.
2+
13
NSubstituteAutoMocker
24
=====================
35

@@ -95,4 +97,4 @@ There may be scenarios where you want to override the autogenerated parameters.
9597
Final note
9698
----------
9799

98-
This extension has been built with unit testing in mind. Paying tribute to NSubstitute it has been designed to keep things simple. There are much more powerful automockers that make use of Dependency Injection containers, this is not one of them.
100+
This extension has been built with unit testing in mind. Paying tribute to NSubstitute it has been designed to keep things simple. There are much more powerful automockers that make use of Dependency Injection containers, this is not one of them.

source/AutoMockerNSubstitute.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMockerNSubstitute", "NSubstituteAutoMocker\AutoMockerNSubstitute.csproj", "{002D064D-9396-4959-BB01-40AEEEF24B32}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMockerNSubstitute.UnitTests", "NSubstituteAutoMocker.UnitTests\AutoMockerNSubstitute.UnitTests.csproj", "{2F299790-5F2F-46F8-9238-181AEAC5CEE0}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{002D064D-9396-4959-BB01-40AEEEF24B32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{002D064D-9396-4959-BB01-40AEEEF24B32}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{002D064D-9396-4959-BB01-40AEEEF24B32}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{002D064D-9396-4959-BB01-40AEEEF24B32}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{2F299790-5F2F-46F8-9238-181AEAC5CEE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{2F299790-5F2F-46F8-9238-181AEAC5CEE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{2F299790-5F2F-46F8-9238-181AEAC5CEE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{2F299790-5F2F-46F8-9238-181AEAC5CEE0}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.4">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
18+
<PackageReference Include="xunit" Version="2.9.3" />
19+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\NSubstituteAutoMocker\AutoMockerNSubstitute.csproj" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<Using Include="Xunit" />
31+
</ItemGroup>
32+
33+
</Project>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using AutoMockerNSubstitute.UnitTests.SamplesToTest;
2+
using NSubstituteAutoMocker;
3+
4+
namespace AutoMockerNSubstitute.UnitTests;
5+
6+
public class NSubstituteAutoMockerTests
7+
{
8+
public class DefaultConstructor
9+
{
10+
[Fact]
11+
public void UsesDefaultConstructorIfAvailable()
12+
{
13+
NSubstituteAutoMocker<ClassWithJustDefaultConstructor> autoMocker =
14+
new NSubstituteAutoMocker<ClassWithJustDefaultConstructor>();
15+
Assert.NotNull(autoMocker.ClassUnderTest);
16+
}
17+
18+
[Fact]
19+
public void ThrowsExceptionIfPrivate()
20+
{
21+
Assert.Throws<ConstructorMatchException>(() => new NSubstituteAutoMocker<ClassWithPrivateDefaultConstructor>());
22+
}
23+
24+
[Fact]
25+
public void ClassUnderTestCanBeSealed()
26+
{
27+
NSubstituteAutoMocker<SealedClass> autoMocker = new NSubstituteAutoMocker<SealedClass>();
28+
29+
Assert.NotNull(autoMocker.ClassUnderTest);
30+
}
31+
32+
[Fact]
33+
public void PrimitiveParameterTypesGetSetToDefaultValue()
34+
{
35+
NSubstituteAutoMocker<ClassWithPrimitiveConstructors> autoMocker = new NSubstituteAutoMocker<ClassWithPrimitiveConstructors>();
36+
37+
Assert.Null(autoMocker.ClassUnderTest.IntValue);
38+
Assert.Null(autoMocker.ClassUnderTest.StringValue);
39+
}
40+
}
41+
42+
public class ConstructorWithArgs
43+
{
44+
[Fact]
45+
public void UsesEquivalentConstructorIfAvailable()
46+
{
47+
NSubstituteAutoMocker<ClassWithAllConstructors> autoMocker =
48+
new NSubstituteAutoMocker<ClassWithAllConstructors>([typeof(IDependency1), typeof(IDependency2)]);
49+
Assert.NotNull(autoMocker.ClassUnderTest);
50+
Assert.NotNull(autoMocker.ClassUnderTest.Dependency1);
51+
Assert.NotNull(autoMocker.ClassUnderTest.Dependency2);
52+
}
53+
54+
[Fact]
55+
public void NullParameterUsesDefaultConstructor()
56+
{
57+
NSubstituteAutoMocker<ClassWithAllConstructors> autoMocker =
58+
new NSubstituteAutoMocker<ClassWithAllConstructors>(null, null);
59+
Assert.NotNull(autoMocker.ClassUnderTest);
60+
}
61+
62+
[Fact]
63+
public void EmptyParameterUsesDefaultConstructor()
64+
{
65+
NSubstituteAutoMocker<ClassWithAllConstructors> autoMocker = new NSubstituteAutoMocker<ClassWithAllConstructors>([]);
66+
67+
Assert.NotNull(autoMocker.ClassUnderTest);
68+
}
69+
70+
[Fact]
71+
public void ThrowsExceptionIfNoMatchAvailable()
72+
{
73+
Assert.Throws<ConstructorMatchException>(() => new NSubstituteAutoMocker<ClassWithAllConstructors>([typeof(IDependency2), typeof(IDependency1)]));
74+
}
75+
76+
[Fact]
77+
public void CanOverrideParameterViaEvent()
78+
{
79+
NSubstituteAutoMocker<ClassWithAllConstructors> autoMocker =
80+
new NSubstituteAutoMocker<ClassWithAllConstructors>([typeof(IDependency1), typeof(IDependency2)],
81+
(paramInfo, obj) =>
82+
{
83+
if (paramInfo.ParameterType == typeof(IDependency1))
84+
return new Dependency1VitoImplementation();
85+
return obj;
86+
});
87+
88+
Assert.IsAssignableFrom<Dependency1VitoImplementation>(autoMocker.ClassUnderTest.Dependency1);
89+
}
90+
91+
[Fact]
92+
public void CanOverrideParameterViaEventAlt()
93+
{
94+
NSubstituteAutoMocker<ClassWithAllConstructors> autoMocker =
95+
new NSubstituteAutoMocker<ClassWithAllConstructors>((paramInfo, obj) =>
96+
{
97+
if (paramInfo.ParameterType == typeof(IDependency1))
98+
return new Dependency1VitoImplementation();
99+
return obj;
100+
});
101+
102+
Assert.IsAssignableFrom<Dependency1VitoImplementation>(autoMocker.ClassUnderTest.Dependency1);
103+
}
104+
}
105+
106+
public class Get
107+
{
108+
[Fact]
109+
public void AvailableTypeReturned()
110+
{
111+
NSubstituteAutoMocker<ClassWithAllConstructors> autoMocker = new NSubstituteAutoMocker<ClassWithAllConstructors>();
112+
113+
Assert.NotNull(autoMocker.Get<IDependency1>());
114+
Assert.NotNull(autoMocker.Get<IDependency2>());
115+
}
116+
117+
[Fact]
118+
public void UnavailableTypeThrowsException()
119+
{
120+
NSubstituteAutoMocker<ClassWithAllConstructors> autoMocker = new NSubstituteAutoMocker<ClassWithAllConstructors>();
121+
122+
Assert.Throws<ConstructorParameterNotFoundException>(() => autoMocker.Get<IOverdraft>());
123+
}
124+
125+
[Fact]
126+
public void MultipleAvailableTypesThrowsExceptionIfNameNotSpecified()
127+
{
128+
NSubstituteAutoMocker<ClassWithDuplicateConstructorTypes> autoMocker = new NSubstituteAutoMocker<ClassWithDuplicateConstructorTypes>();
129+
130+
Assert.Throws<ConstructorParameterNotFoundException>(() => autoMocker.Get<IDependency1>());
131+
}
132+
133+
[Fact]
134+
public void MultipleAvailableTypesWithNameReturned()
135+
{
136+
NSubstituteAutoMocker<ClassWithDuplicateConstructorTypes> autoMocker = new NSubstituteAutoMocker<ClassWithDuplicateConstructorTypes>();
137+
138+
Assert.NotNull(autoMocker.Get<IDependency1>("dependencyTwo"));
139+
}
140+
}
141+
142+
// TODO Do I need to test the use of generics or collections?
143+
// TODO Do I need to test the visibility of classes/interfaces?
144+
}
Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,64 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Microsoft.VisualStudio.TestTools.UnitTesting;
7-
using NSubstitute;
1+
using NSubstitute;
2+
using NSubstituteAutoMocker;
83

9-
namespace NSubstituteAutoMocker.UnitTests.DocumentationSnippets
10-
{
11-
public class SavingsAccount
12-
{
13-
private readonly IInterestCalculator _interestCalculator;
14-
15-
public SavingsAccount(IInterestCalculator interestCalculator)
16-
{
17-
_interestCalculator = interestCalculator;
18-
}
19-
20-
public decimal Balance { get; private set; }
4+
namespace AutoMockerNSubstitute.UnitTests.DocumentationSnippets;
215

22-
public void Deposit(decimal amount)
23-
{
24-
Balance += amount;
25-
}
6+
public class SavingsAccount(IInterestCalculator interestCalculator)
7+
{
8+
public decimal Balance { get; private set; }
269

27-
public void WithDraw(decimal amount)
28-
{
29-
Balance -= amount;
30-
}
10+
public void Deposit(decimal amount)
11+
{
12+
Balance += amount;
13+
}
3114

32-
public void ApplyInterest()
33-
{
34-
Balance += _interestCalculator.Calculate();
35-
}
15+
public void WithDraw(decimal amount)
16+
{
17+
Balance -= amount;
3618
}
3719

38-
public interface IInterestCalculator
20+
public void ApplyInterest()
3921
{
40-
decimal Calculate();
22+
Balance += interestCalculator.Calculate();
4123
}
24+
}
25+
26+
public interface IInterestCalculator
27+
{
28+
decimal Calculate();
29+
}
4230

43-
[TestClass]
44-
public class SavingsAccountTests
31+
public class SavingsAccountTests
32+
{
33+
[Fact]
34+
public void ApplyInterestUpdatesTheBalance()
4535
{
46-
[TestMethod]
47-
public void ApplyInterestUpdatesTheBalance()
48-
{
49-
// Arange
50-
IInterestCalculator interestCalculator = Substitute.For<IInterestCalculator>();
51-
interestCalculator.Calculate().Returns(123);
52-
SavingsAccount savingsAccount = new SavingsAccount(interestCalculator);
36+
// Arrange
37+
IInterestCalculator interestCalculator = Substitute.For<IInterestCalculator>();
38+
interestCalculator.Calculate().Returns(123);
39+
SavingsAccount savingsAccount = new SavingsAccount(interestCalculator);
5340

54-
// Act
55-
savingsAccount.ApplyInterest();
41+
// Act
42+
savingsAccount.ApplyInterest();
5643

57-
// Assert
58-
Assert.AreEqual(123, savingsAccount.Balance);
59-
}
44+
// Assert
45+
Assert.Equal(123, savingsAccount.Balance);
6046
}
47+
}
6148

62-
[TestClass]
63-
public class SavingsAccountTestsWithNSubstituteAutoMocker
49+
public class SavingsAccountTestsWithNSubstituteAutoMocker
50+
{
51+
[Fact]
52+
public void ApplyInterestUpdatesTheBalance()
6453
{
65-
[TestMethod]
66-
public void ApplyInterestUpdatesTheBalance()
67-
{
68-
// Arange
69-
var automocker = new NSubstituteAutoMocker<SavingsAccount>();
70-
automocker.Get<IInterestCalculator>().Calculate().Returns(123);
54+
// Arrange
55+
var automocker = new NSubstituteAutoMocker<SavingsAccount>();
56+
automocker.Get<IInterestCalculator>().Calculate().Returns(123);
7157

72-
// Act
73-
automocker.ClassUnderTest.ApplyInterest();
58+
// Act
59+
automocker.ClassUnderTest.ApplyInterest();
7460

75-
// Assert
76-
Assert.AreEqual(123, automocker.ClassUnderTest.Balance);
77-
}
61+
// Assert
62+
Assert.Equal(123, automocker.ClassUnderTest.Balance);
7863
}
7964
}

0 commit comments

Comments
 (0)