Skip to content

Commit f4b2727

Browse files
authored
Migrate documentation samples to NUnit4 (#889)
* Remove ruby and jekyll from readme * Enable NRE in ProtectedExtensions * Update acknowledgements.md * Migrate documentation to NUnit4 * Remove NUnit.Framework.Legacy from tests * Fix typo
1 parent d9e759f commit f4b2727

Some content is hidden

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

44 files changed

+168
-198
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [UPDATE] Migrate documentation validation from build.fsproj to Roslyn code generator
1313
* [NEW] Added NuGet Package README file.
1414
* [UPDATE] Make public api and tests the same for all TFMs
15+
* [UPDATE] Migrate documentation samples to NUnit4
1516

1617
### 5.3.0 (October 2024)
1718

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ Assert.That(eventWasRaised);
128128

129129
NSubstitute and its tests can be compiled and run using Visual Studio, Visual Studio Code or any other editor with .NET support. Note that some tests are marked `[Pending]` and are not meant to pass at present, so it is a good idea to exclude tests in the Pending category from test runs.
130130

131-
There are also build scripts in the `./build` directory for command line builds, and CI configurations in the project root.
132-
133-
To do [full builds](https://github.com/nsubstitute/NSubstitute/wiki/Release-procedure) you'll also need Ruby, as the jekyll gem is used to generate the website.
131+
Release-procedure https://github.com/nsubstitute/NSubstitute/wiki/Release-procedure
134132

135133
### Other libraries you may be interested in
136134

acknowledgements.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The aim of this file is to acknowledge the software projects that have been used
33
# Software distributed with/compiled into NSubstitute
44

55
## Castle.Core
6-
NSubstitute is built on the Castle.Core library, particularly Castle.DynamicProxy which is used for generating proxies for types and intercepting calls made to them so that NSubstitute can record them.
6+
NSubstitute is built on the Castle.Core library, particularly Castle.DynamicProxy which is used for generating proxies for types and intercepting calls made to them so that NSubstitute can record them.
77

88
Castle.Core is maintained by the Castle Project [https://www.castleproject.org/] and is released under the Apache License, Version 2.0 [https://www.apache.org/licenses/LICENSE-2.0.html].
99

@@ -19,13 +19,13 @@ Used for mocking parts of the NSubstitute mocking library for testing. It is dis
1919
Moq is not directly used in NSubstitute, but was a great source of inspiration. Moq pioneered Arrange-Act-Assert (AAA) mocking syntax for .NET, as well as removing the distinction between mocks and stubs, both of which have become important parts of NSubstitute. Moq is available under the BSD license [https://www.opensource.org/licenses/bsd-license.php].
2020

2121
## Jekyll [https://jekyllrb.com/]
22-
Static website generator written in Ruby, used for NSubstitute's website and documentation. Distributed under the MIT license [https://www.opensource.org/licenses/bsd-license.php].
22+
Static website generator written in Ruby, used for NSubstitute's website and documentation. Distributed under the MIT license [https://www.opensource.org/licenses/bsd-license.php]. No longer used since migration to docfx.
2323

2424
## SyntaxHighlighter [https://alexgorbatchev.com/SyntaxHighlighter/]
2525
Open source, JavaScript, client-side code highlighter used for highlighting code samples on the NSubstitute website. Distributed under the MIT License [https://en.wikipedia.org/wiki/MIT_License] and the GPL [https://www.gnu.org/copyleft/lesser.html].
2626

2727
## FAKE [https://fsharp.github.io/FAKE/]
28-
FAKE (F# Make) is used for NSubstitute's build. It is inspired by `make` and `rake`. FAKE is distributed under a dual Apache 2 / MS-PL license [https://github.com/fsharp/FAKE/blob/master/License.txt].
28+
FAKE (F# Make) is used for NSubstitute's build. It is inspired by `make` and `rake`. FAKE is distributed under a dual Apache 2 / MS-PL license [https://github.com/fsharp/FAKE/blob/master/License.txt]. No longer used since migration to source generators.
2929

3030
## Microsoft .NET Framework [https://www.microsoft.com/net/]
3131
NSubstitute is coded in C# and compiled using Microsoft .NET. It can also run and compile under Mono [https://www.mono-project.com], an open source implementation of the open .NET standards for C# and the CLI.

docs/help/actions-with-arguments/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ calculator.Multiply(Arg.Any<int>(), Arg.Do<int>(x => argumentUsed = x));
8484

8585
calculator.Multiply(123, 42);
8686

87-
Assert.AreEqual(42, argumentUsed);
87+
Assert.That(argumentUsed, Is.EqualTo(42));
8888
```
8989

9090
Here we specify that a call to `Multiply` with any first argument should pass the second argument and put it in the `argumentUsed` variable. This can be quite useful when we want to assert several properties on an argument (for types more complex than `int` that is).
@@ -97,7 +97,7 @@ calculator.Multiply(2, 10);
9797
calculator.Multiply(5, 10);
9898
calculator.Multiply(7, 4567); //Will not match our Arg.Do as second arg is not 10
9999
100-
Assert.AreEqual(firstArgsBeingMultiplied, new[] { 2, 5 });
100+
Assert.That(firstArgsBeingMultiplied, Is.EqualTo(new[] { 2, 5 }));
101101
```
102102

103103
Here our `Arg.Do` takes whatever `int` is passed as the first argument to `Multiply` and adds it to a list whenever the second argument is 10.
@@ -124,6 +124,6 @@ var results = new[] {
124124
calculator.Multiply(123, 2) //First arg greater than 0, so spec won't be met.
125125
};
126126

127-
Assert.AreEqual(3, numberOfCallsWhereFirstArgIsLessThan0); //3 of 4 calls have first arg < 0
128-
Assert.AreEqual(results, new[] {123, 123, 123, 0}); //Last call returns 0, not 123
127+
Assert.That(3, Is.EqualTo(numberOfCallsWhereFirstArgIsLessThan0)); //3 of 4 calls have first arg < 0
128+
Assert.That(results, Is.EqualTo(new[] {123, 123, 123, 0})); //Last call returns 0, not 123
129129
```

docs/help/argument-matchers/index.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ An argument of type `T` can be ignored using `Arg.Any<T>()`.
4848
```csharp
4949
calculator.Add(Arg.Any<int>(), 5).Returns(7);
5050

51-
Assert.AreEqual(7, calculator.Add(42, 5));
52-
Assert.AreEqual(7, calculator.Add(123, 5));
53-
Assert.AreNotEqual(7, calculator.Add(1, 7));
51+
Assert.That(calculator.Add(42, 5), Is.EqualTo(7));
52+
Assert.That(calculator.Add(123, 5), Is.EqualTo(7));
53+
Assert.That(calculator.Add(1, 7), Is.Not.EqualTo(7));
5454
```
5555

5656
In this example we return `7` when adding any number to `5`. We use `Arg.Any<int>()` to tell NSubstitute to ignore the first argument.
@@ -87,11 +87,11 @@ If the condition throws an exception for an argument, then it will be assumed th
8787
```csharp
8888
formatter.Format(Arg.Is<string>(x => x.Length <= 10)).Returns("matched");
8989

90-
Assert.AreEqual("matched", formatter.Format("short"));
91-
Assert.AreNotEqual("matched", formatter.Format("not matched, too long"));
90+
Assert.That(formatter.Format("short"), Is.EqualTo("matched"));
91+
Assert.That(formatter.Format("not matched, too long"), Is.Not.EqualTo("matched"));
9292
// Will not match because trying to access .Length on null will throw an exception when testing
9393
// our condition. NSubstitute will assume it does not match and swallow the exception.
94-
Assert.AreNotEqual("matched", formatter.Format(null));
94+
Assert.That(formatter.Format(null), Is.Not.EqualTo("matched"));
9595
```
9696

9797
## Matching a specific argument
@@ -121,8 +121,8 @@ calculator
121121
});
122122

123123
var hasEntry = calculator.LoadMemory(1, out var memoryValue);
124-
Assert.AreEqual(true, hasEntry);
125-
Assert.AreEqual(42, memoryValue);
124+
Assert.That(hasEntry, Is.EqualTo(true));
125+
Assert.That(memoryValue, Is.EqualTo(42));
126126
```
127127

128128
See [Setting out and ref args](/help/setting-out-and-ref-arguments/) for more information on working with `out` and `ref`.
@@ -204,8 +204,8 @@ widgetFactory.Make(Arg.Do<WidgetInfo>(info => testLog2.Add(info.Name)));
204204
subject.StartWithWidget(new WidgetInfo { Name = "Test Widget" });
205205

206206
/* ASSERT */
207-
Assert.AreEqual(new[] { "Test Widget" }, testLog);
208-
Assert.AreEqual(new[] { "Test Widget" }, testLog2);
207+
Assert.That(testLog, Is.EqualTo(new[] { "Test Widget" }));
208+
Assert.That(testLog2, Is.EqualTo(new[] { "Test Widget" }));
209209
```
210210

211211
### Modifying values being matched
@@ -288,7 +288,7 @@ public void ManualArgSnapshot() {
288288
lookup.Add(person);
289289
person.Name = "Vimes";
290290

291-
Assert.AreEqual("Carrot", namesAdded[0]);
291+
Assert.That(namesAdded[0], Is.EqualTo("Carrot"));
292292
}
293293
```
294294

docs/help/auto-and-recursive-mocks/index.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ var parser = Substitute.For<INumberParser>();
2727
factory.Create(',').Returns(parser);
2828
parser.Parse("an expression").Returns(new[] {1,2,3});
2929

30-
Assert.AreEqual(
30+
Assert.That(
3131
factory.Create(',').Parse("an expression"),
32-
new[] {1,2,3});
32+
Is.EqualTo(new[] {1,2,3}));
3333
```
3434

3535
Or we could use the fact that a substitute for type `INumberParser` will automatically be returned for `INumberParserFactory.Create()`:
@@ -38,9 +38,9 @@ Or we could use the fact that a substitute for type `INumberParser` will automat
3838
var factory = Substitute.For<INumberParserFactory>();
3939
factory.Create(',').Parse("an expression").Returns(new[] {1,2,3});
4040

41-
Assert.AreEqual(
41+
Assert.That(
4242
factory.Create(',').Parse("an expression"),
43-
new[] {1,2,3});
43+
Is.EqualTo(new[] {1,2,3}));
4444
```
4545

4646
Each time a recursively-subbed property or method is called with the same arguments it will return the same substitute. If a method is called with different arguments a new substitute will be returned.
@@ -57,8 +57,8 @@ var firstCall = factory.Create(',');
5757
var secondCall = factory.Create(',');
5858
var thirdCallWithDiffArg = factory.Create('x');
5959

60-
Assert.AreSame(firstCall, secondCall);
61-
Assert.AreNotSame(firstCall, thirdCallWithDiffArg);
60+
Assert.That(firstCall, Is.SameAs(secondCall));
61+
Assert.That(firstCall, Is.Not.SameAs(thirdCallWithDiffArg));
6262
```
6363

6464
_Note:_ Recursive substitutes will not be created for non-purely virtual classes, as creating and using classes can have potentially unwanted side-effects. You'll therefore need to create and return these explicitly.
@@ -86,9 +86,9 @@ To get the identity of the `CurrentRequest` to return a certain name, we could m
8686
```csharp
8787
var context = Substitute.For<IContext>();
8888
context.CurrentRequest.Identity.Name.Returns("My pet fish Eric");
89-
Assert.AreEqual(
90-
"My pet fish Eric",
91-
context.CurrentRequest.Identity.Name);
89+
Assert.That(
90+
context.CurrentRequest.Identity.Name,
91+
Is.EqualTo("My pet fish Eric"));
9292
```
9393

9494
Here `CurrentRequest` is automatically going to return a substitute for `IRequest`, and the `IRequest` substitute will automatically return a substitute for `IIdentity`.
@@ -100,6 +100,7 @@ Properties and methods returning types of `String` or `Array` will automatically
100100

101101
```csharp
102102
var identity = Substitute.For<IIdentity>();
103-
Assert.AreEqual(String.Empty, identity.Name);
104-
Assert.AreEqual(0, identity.Roles().Length);
103+
104+
Assert.That(identity.Name, Is.EqualTo(String.Empty));
105+
Assert.That(identity.Roles().Length, Is.EqualTo(0));
105106
```

docs/help/callbacks/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ calculator
2525
calculator.Add(7, 3);
2626
calculator.Add(2, 2);
2727
calculator.Add(11, -3);
28-
Assert.AreEqual(counter, 3);
28+
Assert.That(counter, Is.EqualTo(3));
2929
```
3030

3131
The [Return from a function](/help/return-from-function) topic has more information on the arguments passed to the callback.
@@ -51,7 +51,7 @@ public void SayHello() {
5151

5252
foo.SayHello("World");
5353
foo.SayHello("World");
54-
Assert.AreEqual(2, counter);
54+
Assert.That(counter, Is.EqualTo(2));
5555
}
5656
```
5757

@@ -67,8 +67,8 @@ calculator
6767
.Do(x => counter++);
6868

6969
var result = calculator.Add(1, 2);
70-
Assert.AreEqual(3, result);
71-
Assert.AreEqual(1, counter);
70+
Assert.That(result, Is.EqualTo(3));
71+
Assert.That(counter, Is.EqualTo(1));
7272
```
7373

7474
## Per argument callbacks

docs/help/configure/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ calculator.Add(Arg.Any<int>(), Arg.Any<int>()).Returns(x => { throw new Exceptio
2525
calculator.Configure().Add(1, 2).Returns(3);
2626

2727
// Now both the exception callback and our other return have been configured:
28-
Assert.AreEqual(3, calculator.Add(1, 2));
28+
Assert.That(calculator.Add(1, 2), Is.EqualTo(3));
2929
Assert.Throws<Exception>(() => calculator.Add(-2, -2));
3030
```
3131

docs/help/creating-a-substitute/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ var substitute = Substitute.For(
4949
new[] { typeof(ICommand), typeof(ISomeInterface), typeof(SomeClassWithCtorArgs) },
5050
new object[] { 5, "hello world" }
5151
);
52-
Assert.IsInstanceOf<ICommand>(substitute);
53-
Assert.IsInstanceOf<ISomeInterface>(substitute);
54-
Assert.IsInstanceOf<SomeClassWithCtorArgs>(substitute);
52+
Assert.That(substitute, Is.InstanceOf<ICommand>());
53+
Assert.That(substitute, Is.InstanceOf<ISomeInterface>());
54+
Assert.That(substitute, Is.InstanceOf<SomeClassWithCtorArgs>());
5555
```
5656

5757
<!--
@@ -92,7 +92,7 @@ NSubstitute can also substitute for delegate types by using `Substiute.For<T>()`
9292
var func = Substitute.For<Func<string>>();
9393

9494
func().Returns("hello");
95-
Assert.AreEqual("hello", func());
95+
Assert.That(func(), Is.EqualTo("hello"));
9696
```
9797

9898
## Partial substitutes and test spies

docs/help/multiple-returns/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ A call can also be configured to return a different value over multiple calls. T
1717

1818
```csharp
1919
calculator.Mode.Returns("DEC", "HEX", "BIN");
20-
Assert.AreEqual("DEC", calculator.Mode);
21-
Assert.AreEqual("HEX", calculator.Mode);
22-
Assert.AreEqual("BIN", calculator.Mode);
20+
Assert.That(calculator.Mode, Is.EqualTo("DEC"));
21+
Assert.That(calculator.Mode, Is.EqualTo("HEX"));
22+
Assert.That(calculator.Mode, Is.EqualTo("BIN"));
2323
```
2424

2525
This can also be achieved by [returning from a function](/help/return-from-function), but passing multiple values to `Returns()` is simpler and reads better.
@@ -30,8 +30,8 @@ This can also be achieved by [returning from a function](/help/return-from-funct
3030

3131
```csharp
3232
calculator.Mode.Returns(x => "DEC", x => "HEX", x => { throw new Exception(); });
33-
Assert.AreEqual("DEC", calculator.Mode);
34-
Assert.AreEqual("HEX", calculator.Mode);
33+
Assert.That(calculator.Mode, Is.EqualTo("DEC"));
34+
Assert.That(calculator.Mode, Is.EqualTo("HEX"));
3535
Assert.Throws<Exception>(() => { var result = calculator.Mode; });
3636
```
3737

0 commit comments

Comments
 (0)