Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: introduce clickable copy to clipboard headers for all pages and a hx-tags #729

Merged
merged 19 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ DEPENDENCIES
ruby_dep (~> 1.3)

BUNDLED WITH
1.17.2
1.17.2
1 change: 1 addition & 0 deletions docs/_layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8" />
<title>NSubstitute: {{ page.title }}</title>
<link rel="stylesheet" href="/css/style.css" type="text/css" />
<script src="{{ base.url | prepend: site.url }}/js/scripts.js"></script>
</head>
<body>
<div id="header">
Expand Down
2 changes: 1 addition & 1 deletion docs/_layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>

<div id="post-content">
<h1>{{ page.title }}</h1>
<h1><a href="{{ post.url }}#">{{ page.title }}</a></h1>
{{ content }}
<div id="page-navigation">
<div class="previous">
Expand Down
66 changes: 51 additions & 15 deletions docs/css/style.css
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
html { font-family: "helvetica", Helvetica, Arial }
body {
margin:0;
padding:0;
border:0;
width:100%;
min-width:600px;
margin: 0;
padding: 0;
border: 0;
width: 100%;
min-width: 600px;
}

p{
font-size:1.0em;
line-height:1.6;
p {
font-size: 1.0em;
line-height: 1.6;
}

ul {
font-size:1.0em;
line-height:1.6;
font-size: 1.0em;
line-height: 1.6;
}

h1{font-size:2.5em}
h2{font-size:2.0em}
h3{font-size:1.5em}
h1{font-size: 2.5em}
h2{font-size: 2.0em}
h3{font-size: 1.5em}

a:link { color: #365f91; text-decoration: none;}
a:visited { color: #365f91; text-decoration: none;}
Expand Down Expand Up @@ -80,7 +80,7 @@ a:active { color: #000000; text-decoration: underline;}
#post-content {
float: left;
position: relative;
width:71%;
width: 71%;
padding-left: 10px;
}

Expand Down Expand Up @@ -181,6 +181,42 @@ code {
min-height: inherit !important;
}

h1 {
margin-top: 0.5em;
margin-bottom: 0.2em;
}

h2 {
position: relative;
display: inline-block;
margin-top: 0.5em;
margin-bottom: 0.2em;
}

h3 {
position: relative;
display: inline-block;
margin-top: 0.5em;
margin-bottom: 0.2em;
}

.tooltip {
visibility: visible;
color: #365f91;
background-color: #ECF0F5;
position: absolute;
z-index: 1;
white-space: nowrap;
font-size: 0.4em;
padding: 0.6em;
margin: 0.4em auto auto 0.6em;
border-radius: 30px;
}

.tooltip-hidden {
visibility: hidden;
}

/* Pygments vs style, as generated by:
* https://github.com/richleland/pygments-css/blob/master/vs.css
*/
Expand Down Expand Up @@ -222,4 +258,4 @@ code {
.highlight .sr { color: #a31515 } /* Literal.String.Regex */
.highlight .s1 { color: #a31515 } /* Literal.String.Single */
.highlight .ss { color: #a31515 } /* Literal.String.Symbol */
/* End pygments vs style */
/* End pygments vs style */
4 changes: 2 additions & 2 deletions docs/help/_posts/2010-01-01-getting-started.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Getting started
layout: post
---

## Adding NSubstitute to your test project
## [Adding NSubstitute to your test project](#adding-nsubstitute-to-your-test-project)

dtchepak marked this conversation as resolved.
Show resolved Hide resolved
First add the [NSubstitute NuGet package](https://nuget.org/List/Packages/NSubstitute) to your test project using [NuGet](https://docs.microsoft.com/en-us/nuget/quickstart/use-a-package) (either the command line executable, or via the package manager in your IDE).

Expand All @@ -15,7 +15,7 @@ It is optional but recommended to also install [NSubstitute.Analyzers.CSharp](ht
// or
> Install-Package NSubstitute.Analyzers.VisualBasic

## Using NSubstitute in a test fixture
## [Using NSubstitute in a test fixture](#using-nsubstitute-in-a-test-fixture)

So now you are staring at a blank test fixture (created with your favourite unit testing framework; for these examples we're using [NUnit](https://nunit.org/)), and are wondering where to start.

Expand Down
8 changes: 4 additions & 4 deletions docs/help/_posts/2010-01-02-creating-a-substitute.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var substitute = Substitute.For<ISomeInterface>();

This is how you'll normally create substitutes for types. Generally this type will be an interface, but you can also substitute classes in cases of emergency.

## Substituting (infrequently and carefully) for classes
## [Substituting (infrequently and carefully) for classes](#substituting-infrequently-and-carefully-for-classes)

⚠️ **Warning:** Substituting for classes can have some nasty side-effects!

Expand All @@ -29,7 +29,7 @@ var someClass = Substitute.For<SomeClassWithCtorArgs>(5, "hello world");

For classes that have default constructors the syntax is the same as substituting for interfaces.

## Substituting for multiple interfaces
## [Substituting for multiple interfaces](#substituting-for-multiple-interfaces)

There are times when you want to substitute for multiple types. The best example of this is when you have code that works with a type, then checks whether it implements <code>IDisposable</code> and disposes of it if it doesn't.

Expand Down Expand Up @@ -85,7 +85,7 @@ public class CommandRunner
```
-->

## Substituting for delegates
## [Substituting for delegates](#substituting-for-delegates)

NSubstitute can also substitute for delegate types by using `Substiute.For<T>()`. When substituting for delegate types you will not be able to get the substitute to implement additional interfaces or classes.

Expand All @@ -96,7 +96,7 @@ func().Returns("hello");
Assert.AreEqual("hello", func());
```

## Partial substitutes and test spies
## [Partial substitutes and test spies](#partial-substitutes-and-test-spies)

When required we can also create substitutes that run real code by default, letting us replace [specific parts of a class with substitute behaviour](/help/partial-subs/).

Expand Down
6 changes: 3 additions & 3 deletions docs/help/_posts/2010-02-01-set-return-value.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ICalculator calculator;
```
-->

## For methods
## [For methods](#for-methods)
To set a return value for a method call on a substitute, call the method as normal, then follow it with a call to NSubstitute's `Returns()` extension method.

```csharp
Expand All @@ -39,7 +39,7 @@ Assert.AreEqual(calculator.Add(1, 2), 3);
Assert.AreNotEqual(calculator.Add(3, 6), 3);
```

## For properties
## [For properties](#for-properties)
The return value for a property can be set in the same way as for a method, using `Returns()`. You can also just use plain old property setters for read/write properties; they'll behave just the way you expect them to.

```csharp
Expand All @@ -51,6 +51,6 @@ Assert.AreEqual(calculator.Mode, "HEX");
```


## More ways of setting return values
## [More ways of setting return values](#more-ways-of-setting-return-values)
This covers the very basics of setting a return value, but NSubstitute can do much more. Read on for other approachs, including [matching specific arguments](/help/return-for-args), [ignoring arguments](/help/return-for-any-args), using [functions to calculate return values](/help/return-from-function) and returning [multiple results](/help/multiple-returns).

4 changes: 2 additions & 2 deletions docs/help/_posts/2010-02-03-return-from-function.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Assert.That(calculator.Add(-73, 9348), Is.EqualTo(9275));

In this example [argument matchers](/help/argument-matchers) are used to match all calls to `Add()`, and a lambda function is used to return the sum of the first and second arguments passed to the call.

### Call information
### [Call information](#call-information)
The function we provide to `Returns()` and `ReturnsForAnyArgs()` is of type `Func<CallInfo,T>`, where `T` is the type the call is returning, and `CallInfo` is a type which provides access to the arguments used for the call. In the previous example we accessed these arguments using an indexer (`x[1]` for the second argument). `CallInfo` also has a couple of convenience methods to pick arguments in a strongly typed way:

* `T Arg<T>()`: Gets the argument of type `T` passed to this call.
Expand All @@ -48,7 +48,7 @@ Assert.That(foo.Bar(1, "World"), Is.EqualTo("Hello World"));

Here `x.Arg<string>()` will return the `string` argument passed to the call, rather than having to use `(string) x[1]`. If there are two `string` arguments to a call, NSubstitute will throw an exception and let you know that it can't work out which argument you mean.

### Callbacks
### [Callbacks](#callbacks)

This technique can also be used to get a callback whenever a call is made:

Expand Down
4 changes: 2 additions & 2 deletions docs/help/_posts/2010-02-04-multiple-returns.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Assert.AreEqual("BIN", calculator.Mode);

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.

## Multiple returns using callbacks
## [Multiple returns using callbacks](#multiple-returns-using-callbacks)

`Returns()` also supports passing multiple [functions to return from](/help/return-from-function), which allows one call in a sequence to throw an exception or perform some other action.

Expand All @@ -36,7 +36,7 @@ Assert.AreEqual("HEX", calculator.Mode);
Assert.Throws<Exception>(() => { var result = calculator.Mode; });
```

## Configuring other calls without using up multiple returns
## [Configuring other calls without using up multiple returns](#configuring-other-calls-without-using-up-multiple-returns)

If a call has been configured with multiple returns values, you can configure a more specific call without using up any of these callbacks using [`.Configure()`](/help/configure/).

16 changes: 8 additions & 8 deletions docs/help/_posts/2010-03-01-received-calls.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void Should_execute_command() {

In this case `command` did receive a call to `Execute()`, and so will complete successfully. If `Execute()` has not been received NSubstitute will throw a `ReceivedCallsException` and let you know what call was expected and with which arguments, as well as listing actual calls to that method and which the arguments differed.

## Check a call was not received
## [Check a call was not received](#check-a-call-was-not-received)
NSubstitute can also make sure a call was not received using the `DidNotReceive()` extension method.

```csharp
Expand All @@ -46,7 +46,7 @@ something.DontDoAnything();
command.DidNotReceive().Execute();
```

## Check a call was received a specific number of times
## [Check a call was received a specific number of times](#check-a-call-was-received-a-specific-number-of-times)

The `Received()` extension method will assert that at least one call was made to a member, and `DidNotReceive()` asserts that zero calls were made. NSubstitute also gives you the option of asserting a specific number of calls were received by passing an integer to `Received()`. This will throw if the substitute does not receive *exactly* that many matching calls. Too few, or too many, and the assertion will fail.

Expand Down Expand Up @@ -77,7 +77,7 @@ public void Should_execute_command_the_number_of_times_specified() {

We can also use `Received(1)` to check a call was received once and only once. This differs from the standard `Received()` call, which checks a call was received *at least* once. `Received(0)` behaves the same as `DidNotReceive()`.

## Received (or not) with specific arguments
## [Received (or not) with specific arguments](#received-or-not-with-specific-arguments)

<!--
```requiredcode
Expand Down Expand Up @@ -107,7 +107,7 @@ calculator
.Add(Arg.Any<int>(), Arg.Is<int>(x => x >= 500));
```

## Ignoring arguments
## [Ignoring arguments](#ignoring-arguments)

NSubstitute can also check calls were received or not received but ignore the arguments used, just like we can for [setting returns for any arguments](/help/return-for-any-args). In this case we need `ReceivedWithAnyArgs()` and `DidNotReceiveWithAnyArgs()`.

Expand All @@ -118,7 +118,7 @@ calculator.ReceivedWithAnyArgs().Add(default, default);
calculator.DidNotReceiveWithAnyArgs().Subtract(default, default);
```

## Checking calls to properties
## [Checking calls to properties](#checking-calls-to-properties)

The same syntax can be used to check calls on properties. Normally we'd want to avoid this, as we're really more interested in testing the required behaviour rather than the precise implementation details (i.e. we would set the property to return a value and check that was used properly, rather than assert that the property getter was called). Still, there are probably times when checking getters and setters were called can come in handy, so here's how you do it:

Expand All @@ -135,7 +135,7 @@ _ = calculator.Received().Mode;
calculator.Received().Mode = "TEST";
```

## Checking calls to indexers
## [Checking calls to indexers](#checking-calls-to-indexers)
An indexer is really just another property, so we can use the same syntax to check calls to indexers.

```csharp
Expand All @@ -146,7 +146,7 @@ dictionary.Received()["test"] = 1;
dictionary.Received()["test"] = Arg.Is<int>(x => x < 5);
```

## Checking event subscriptions
## [Checking event subscriptions](#checking-event-subscriptions)

As with properties, we'd normally favour testing the required behaviour over checking subscriptions to particular event handlers. We can do that by [raising an event on the substitute](/help/raising-events/) and asserting our class performs the correct behaviour in response:

Expand Down Expand Up @@ -187,7 +187,7 @@ public void MakeSureWatcherSubscribesToCommandExecuted() {
}
```

## Checking event invocation
## [Checking event invocation](#checking-event-invocation)

We can also use substitutes for event handlers to confirm that a particular event was raised correctly. Often a simple lambda function will suffice, but if we want to use argument matchers we can use a substitute and `Received`. Both options are shown below:

Expand Down
14 changes: 7 additions & 7 deletions docs/help/_posts/2010-04-01-argument-matchers.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ string TestWidget = "test widget";
```
-->

## Ignoring arguments
## [Ignoring arguments](#ignoring-arguments)
An argument of type `T` can be ignored using `Arg.Any<T>()`.

```csharp
Expand All @@ -67,7 +67,7 @@ formatter.Received().Format(Arg.Any<string>());
formatter.DidNotReceive().Format(Arg.Any<int>());
```

## Conditionally matching an argument
## [Conditionally matching an argument](#conditionally-matching-an-argument)
An argument of type `T` can be conditionally matched using `Arg.Is<T>(Predicate<T> condition)`.

```csharp
Expand Down Expand Up @@ -95,7 +95,7 @@ Assert.AreNotEqual("matched", formatter.Format("not matched, too long"));
Assert.AreNotEqual("matched", formatter.Format(null));
```

## Matching a specific argument
## [Matching a specific argument](#matching-a-specific-argument)
An argument of type `T` can be matched using `Arg.Is<T>(T value)`.

```csharp
Expand All @@ -109,7 +109,7 @@ calculator.Received().Add(Arg.Is(0), Arg.Any<int>());

This matcher normally isn't required; most of the time we can just use `0` instead of `Arg.Is(0)`. In some cases though, NSubstitute can't work out which matcher applies to which argument (arg matchers are actually fuzzily matched; not passed directly to the function call). In these cases it will throw an `AmbiguousArgumentsException` and ask you to specify one or more additional argument matchers. In some cases you may have to explicitly use argument matchers for every argument.

## Matching `out` and `ref` args
## [Matching `out` and `ref` args](#matching-out-and-ref-args)

Argument matchers can also be used with `out` and `ref` (NSubstitute 4.0 and later with C# 7.0 and later).

Expand All @@ -128,11 +128,11 @@ Assert.AreEqual(42, memoryValue);

See [Setting out and ref args](/help/setting-out-and-ref-arguments/) for more information on working with `out` and `ref`.

## How NOT to use argument matchers
## [How NOT to use argument matchers](#how-not-to-use-argument-matchers)

Occasionally argument matchers get used in ways that cause unexpected results for people. Here are the most common ones.

### Using matchers outside of stubbing or checking received calls
### [Using matchers outside of stubbing or checking received calls](#using-matchers-outside-of-stubbing-or-checking-received-calls)

Argument matchers should only be used when specifying calls for the purposes of setting return values, checking received calls, or configuring callbacks (for example: with `Returns`, `Received` or `When`). Using `Arg.Is` or `Arg.Any` in other situations can cause your tests to behave in unexpected ways.

Expand Down Expand Up @@ -209,7 +209,7 @@ Assert.AreEqual(new[] { "Test Widget" }, testLog);
Assert.AreEqual(new[] { "Test Widget" }, testLog2);
```

### Modifying values being matched
### [Modifying values being matched](#modifying-values-being-matched)

When NSubstitute records calls, it keeps a reference to the arguments passed, not a deep clone of each argument at the time of the call. This means that if the properties of an argument change after the call assertions may not behave as expected.

Expand Down
8 changes: 4 additions & 4 deletions docs/help/_posts/2010-05-01-callbacks.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Assert.AreEqual(counter, 3);

The [Return from a function](/help/return-from-function) topic has more information on the arguments passed to the callback.

## Callbacks for `void` calls
## [Callbacks for `void` calls](#callbacks-for-void-calls)

`Returns()` can be used to get callbacks for members that return a value, but for `void` members we need a different technique, because we can't call a method on a `void` return. For these cases we can use the `When..Do` syntax.

## When called, do this
## [When called, do this](#when-called-do-this)

`When..Do` uses two calls to configure our callback. First, `When()` is called on the substitute and passed a function. The argument to the function is the substitute itself, and we can call the member we are interested in here, even if it returns `void`. We then call `Do()` and pass in our callback that will be executed when the substitute's member is called.

Expand Down Expand Up @@ -72,14 +72,14 @@ Assert.AreEqual(3, result);
Assert.AreEqual(1, counter);
```

## Per argument callbacks
## [Per argument callbacks](#per-argument-callbacks)

In cases where we only need callbacks for a particular argument we may be able to use [per argument callbacks like `Arg.Do()` and `Arg.Invoke()`](/help/actions-with-arguments) instead of `When..Do`.

Argument callbacks give us slightly more concise code in a style that is more in keeping with the rest of the NSubstitute API. See [Actions with arguments](/help/actions-with-arguments) for more information and examples.


## Callback builder for more complex callbacks
## [Callback builder for more complex callbacks](#callback-builder-for-more-complex-callbacks)

The `Callback` builder lets us create more complex `Do()` scenarios. We can use `Callback.First()` followed by `Then()`, `ThenThrow()` and `ThenKeepDoing()` to build chains of callbacks. We can also use `Always()` and `AlwaysThrow()` to specify callbacks called every time. Note that a callback set by an `Always()` method will be called even if other callbacks will throw an exception.

Expand Down
Loading