Skip to content

Commit c250356

Browse files
committed
upgrade to .NET 10, slnx, NUnit 4, add GitHub CI
1 parent 2b205c6 commit c250356

File tree

12 files changed

+93
-192
lines changed

12 files changed

+93
-192
lines changed

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Setup .NET
17+
uses: actions/setup-dotnet@v4
18+
with:
19+
dotnet-version: '10.0.x'
20+
21+
- name: Restore
22+
run: dotnet restore NSubstituteTutorial.slnx
23+
24+
- name: Build
25+
run: dotnet build NSubstituteTutorial.slnx --no-restore
26+
27+
- name: Test
28+
run: dotnet test NSubstituteTutorial.slnx --no-build --verbosity normal

NSubstituteTutorial.sln

Lines changed: 0 additions & 28 deletions
This file was deleted.

NSubstituteTutorial.slnx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Solution>
2+
<Project Path="nsubstitute\nsubstitute-tut.csproj" />
3+
<Folder Name="/Solution Items/">
4+
<File Path=".gitignore" />
5+
<File Path="README.md" />
6+
</Folder>
7+
</Solution>

global.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sdk": {
3+
"version": "10.0.100",
4+
"rollForward": "latestFeature"
5+
}
6+
}

nsubstitute/1-Tutorial.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Security.Claims;
6-
using System.Security.Principal;
7-
using System.Windows.Input;
1+
using System.Security.Principal;
82
using NSubstitute;
93
using NSubstitute.Core;
104
using NSubstitute.Extensions;
@@ -42,7 +36,7 @@ public void CreateSubstitute_ForClass()
4236
}
4337
catch (CouldNotSetReturnDueToTypeMismatchException ex) when (ex.Message.Contains("Return values cannot be configured for non-virtual/non-abstract members"))
4438
{
45-
Assert.True(true, "NSubstitute only works with virtual/abstract members when substituting an actual class");
39+
Assert.That(true, Is.True, "NSubstitute only works with virtual/abstract members when substituting an actual class");
4640
}
4741
}
4842

@@ -99,8 +93,8 @@ public void CreateSubstitute_ForMultipleTypes()
9993
{
10094
// Multiple substitutions (at most one class)
10195
var msub = Substitute.For<ICalculator, IDisposable>();
102-
Assert.IsInstanceOf<ICalculator>(msub);
103-
Assert.IsInstanceOf<IDisposable>(msub);
96+
Assert.That(msub, Is.InstanceOf<ICalculator>());
97+
Assert.That(msub, Is.InstanceOf<IDisposable>());
10498

10599

106100
// For more than three types
@@ -110,8 +104,8 @@ public void CreateSubstitute_ForMultipleTypes()
110104
new[] { typeof(IComparable), typeof(IDisposable), typeof(ICloneable), typeof(Calculator) },
111105
new object[] { 8 } // @base ctor argument for Calculator
112106
);
113-
Assert.IsInstanceOf<IDisposable>(gt3Sub);
114-
Assert.IsInstanceOf<Calculator>(gt3Sub);
107+
Assert.That(gt3Sub, Is.InstanceOf<IDisposable>());
108+
Assert.That(gt3Sub, Is.InstanceOf<Calculator>());
115109

116110
var calcer = (Calculator)gt3Sub;
117111
calcer.Add(1, 1).Returns(2);
@@ -198,24 +192,24 @@ public void ProvideValues_WithoutSetup_ReturnsDefaultOrEmpty()
198192

199193
// Default for IEnumerable<T> is Enumerable.Empty<T>()
200194
var testy = Substitute.For<VirtualsTest>();
201-
Assert.That(testy.Virtual.Count(), Is.EqualTo(0));
202-
Assert.Null(testy.NotVirtual);
195+
Assert.That(testy.Virtual!.Count(), Is.EqualTo(0));
196+
Assert.That(testy.NotVirtual, Is.Null);
203197

204198
// Default for string is ""
205199
var identity = Substitute.For<IIdentity>();
206-
Assert.AreEqual("", identity.Name);
200+
Assert.That(identity.Name, Is.EqualTo(""));
207201

208202
// Members returning an interface will automatically return a sub (recursively)
209203
var principal = Substitute.For<IPrincipal>();
210-
IIdentity principalIdentity = principal.Identity;
204+
var principalIdentity = principal.Identity!;
211205
principalIdentity.Name.Returns("Neo");
212206
Assert.That(principalIdentity.Name, Is.EqualTo("Neo"));
213207
}
214208

215209
public class VirtualsTest
216210
{
217-
public virtual IEnumerable<string> Virtual { get; set; }
218-
public IEnumerable<string> NotVirtual { get; set; }
211+
public virtual IEnumerable<string>? Virtual { get; set; }
212+
public IEnumerable<string>? NotVirtual { get; set; }
219213
}
220214

221215
[Test]
@@ -237,18 +231,18 @@ public void ProvideValues_ForMethodsAndProperties()
237231

238232
// Provide multiple return values
239233
nsub.Mode.Returns("DEC", "HEX", "BIN");
240-
Assert.AreEqual("DEC", nsub.Mode);
241-
Assert.AreEqual("HEX", nsub.Mode);
242-
Assert.AreEqual("BIN", nsub.Mode);
234+
Assert.That(nsub.Mode, Is.EqualTo("DEC"));
235+
Assert.That(nsub.Mode, Is.EqualTo("HEX"));
236+
Assert.That(nsub.Mode, Is.EqualTo("BIN"));
243237

244238
// Multiple return values with a function
245239
nsub.Mode.Returns(
246240
x => "DEC",
247241
x => "HEX",
248242
x => throw new Exception()
249243
);
250-
Assert.AreEqual("DEC", nsub.Mode);
251-
Assert.AreEqual("HEX", nsub.Mode);
244+
Assert.That(nsub.Mode, Is.EqualTo("DEC"));
245+
Assert.That(nsub.Mode, Is.EqualTo("HEX"));
252246
Assert.Throws<Exception>(() => { var result = nsub.Mode; });
253247
}
254248

@@ -272,8 +266,8 @@ public void ProvideValues_WithRefAndOut()
272266
return 2;
273267
});
274268

275-
Assert.AreEqual(2, nsub.Divide(12, 5, out remainder));
276-
Assert.AreEqual(0.4F, remainder);
269+
Assert.That(nsub.Divide(12, 5, out remainder), Is.EqualTo(2));
270+
Assert.That(remainder, Is.EqualTo(0.4F));
277271
}
278272

279273
[Test]
@@ -334,7 +328,7 @@ public void ProvideValues_AndDoesCallback()
334328

335329
nsub.Add(7, 3);
336330
nsub.Add(2, 2);
337-
Assert.AreEqual(counter, 2);
331+
Assert.That(counter, Is.EqualTo(2));
338332
}
339333

340334
[Test]
@@ -349,7 +343,7 @@ public void SubstituteVoids_WithWhenDo()
349343
.Do(x => called = true);
350344

351345
sub.SetMode("HEX");
352-
Assert.True(called);
346+
Assert.That(called, Is.True);
353347
}
354348

355349
#region Performing actions with arguments
@@ -362,7 +356,7 @@ public void PerformingActionsWithArguments()
362356
nsub.Add(10, 42);
363357
nsub.Add(11, 0); // does not overwrite argumentUsed because first arg is not 10
364358

365-
Assert.AreEqual(42, argumentUsed);
359+
Assert.That(argumentUsed, Is.EqualTo(42));
366360
}
367361
#endregion
368362
#endregion

nsubstitute/2-Moq-Comparison.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
21
using Moq;
32
using NSubstitute;
4-
using NSubstitute.Core;
53
using NSubstitute.ExceptionExtensions;
64
using NUnit.Framework;
75

@@ -18,19 +16,19 @@ public void BasicExample()
1816
{
1917
// Methods
2018
moq.Setup(calc => calc.Add(1, 1)).Returns(2);
21-
Assert.AreEqual(2, moq.Object.Add(1, 1));
19+
Assert.That(moq.Object.Add(1, 1), Is.EqualTo(2));
2220

2321
nsub.Add(1, 1).Returns(2);
24-
Assert.AreEqual(2, nsub.Add(1, 1));
22+
Assert.That(nsub.Add(1, 1), Is.EqualTo(2));
2523

2624
// Properties
2725
moq.Setup(calc => calc.Mode).Returns("DEC");
28-
Assert.AreEqual("DEC", moq.Object.Mode);
26+
Assert.That(moq.Object.Mode, Is.EqualTo("DEC"));
2927

3028
nsub.Mode = "DEC";
3129
// Can also use the same syntax as for methods:
3230
// nsub.Mode.Returns("DEC");
33-
Assert.AreEqual("DEC", nsub.Mode);
31+
Assert.That(nsub.Mode, Is.EqualTo("DEC"));
3432
}
3533

3634
[Test]
@@ -41,8 +39,8 @@ public void OutAndRef()
4139
moq.Setup(calc => calc.Divide(12, 5, out remainder)).Returns(2);
4240
remainder = 0;
4341

44-
Assert.AreEqual(2, moq.Object.Divide(12, 5, out remainder));
45-
Assert.AreEqual(0.4F, remainder);
42+
Assert.That(moq.Object.Divide(12, 5, out remainder), Is.EqualTo(2));
43+
Assert.That(remainder, Is.EqualTo(0.4F));
4644
}
4745

4846
{
@@ -53,19 +51,19 @@ public void OutAndRef()
5351
});
5452
remainder = 0;
5553

56-
Assert.AreEqual(2, nsub.Divide(12, 5, out remainder));
57-
Assert.AreEqual(0.4F, remainder);
54+
Assert.That(nsub.Divide(12, 5, out remainder), Is.EqualTo(2));
55+
Assert.That(remainder, Is.EqualTo(0.4F));
5856
}
5957
}
6058

6159
[Test]
6260
public void ManipulateResult()
6361
{
6462
moq.Setup(calc => calc.Add(1, 1)).Returns((int a, int b) => a + b + 1);
65-
Assert.AreEqual(3, moq.Object.Add(1, 1));
63+
Assert.That(moq.Object.Add(1, 1), Is.EqualTo(3));
6664

6765
nsub.Add(1, 1).Returns(r => (int) r[0] + (int) r[1] + 1);
68-
Assert.AreEqual(3, nsub.Add(1, 1));
66+
Assert.That(nsub.Add(1, 1), Is.EqualTo(3));
6967
}
7068

7169
[Test]
@@ -87,8 +85,9 @@ public void ThrowExceptions()
8785

8886
// The extension method syntax is much cleaner
8987
// Without it, the setup becomes the following
90-
nsub.Add(1, 1).Returns(x => { throw new InvalidOperationException(); });
91-
nsub.When(x => x.SetMode("HEX")).Do(x => { throw new ArgumentException(); });
88+
var nsub2 = Substitute.For<ICalculator>();
89+
nsub2.Add(1, 1).Returns(x => { throw new InvalidOperationException(); });
90+
nsub2.When(x => x.SetMode("HEX")).Do(x => { throw new ArgumentException(); });
9291
}
9392

9493
[Test]

nsubstitute/3-Not-Covered.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.Diagnostics;
4-
using System.Linq;
52
using NSubstitute;
63
using NSubstitute.Core;
7-
using NSubstitute.Exceptions;
84
using NUnit.Framework;
95

106
namespace NSubstituteTutorial
@@ -78,7 +74,7 @@ public void InvokeArgumentCallbacks()
7874
var command = new OrderPlacedCommand(processor);
7975
command.Execute();
8076

81-
Assert.True(command.OkCalled);
77+
Assert.That(command.OkCalled, Is.True);
8278
}
8379
#endregion
8480

nsubstitute/Calculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public int Divide(int n, int divisor, out float remainder)
2323
return n / divisor;
2424
}
2525

26-
public string Mode { get; set; }
26+
public string Mode { get; set; } = "";
2727

2828
public void SetMode(string mode)
2929
{

nsubstitute/ICalculator.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
31
namespace NSubstituteTutorial
42
{
53
public interface ICalculator

nsubstitute/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)