Skip to content

Commit 8720e25

Browse files
authored
Merge branch 'main' into throws-async
2 parents cdc9fc1 + 992f2d8 commit 8720e25

17 files changed

Lines changed: 127 additions & 40 deletions

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: csharp
22
dist: xenial
3-
dotnet: 5.0.100
3+
dotnet: 6.0.100
44
mono: none
5-
env: CONFIGURATION=Release FRAMEWORK=net5.0
5+
env: CONFIGURATION=Release FRAMEWORK=net6.0
66

77
before_script:
88
- dotnet --info

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
### unreleased
2-
* [NEW] Add .NET 5 support
32
* [NEW] Add `.ThrowsAsync()` that will correctly mock exception on async methods. (#609)
43

4+
### 4.3.0 (Jan 2021)
5+
6+
* [NEW] Add .NET 5 (#636) and .NET 6 (#674) support. Thanks to @zvirja and @Havunen!
7+
58
### 4.2.2 (Jun 2020)
69

710
* [UPDATE] Build update to run on non-Windows platforms. Thanks @joaopgrassi! (#592, #335)

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<PropertyGroup>
88
<TargetIsNetFx Condition="$(TargetFramework.StartsWith('net4'))">true</TargetIsNetFx>
9-
<TargetIsNet5 Condition="'$(TargetFramework)' == 'net5.0'">true</TargetIsNet5>
9+
<TargetIsNet5OrNewer Condition="'$(TargetFramework)' == 'net5.0' Or '$(TargetFramework)' == 'net6.0'">true</TargetIsNet5OrNewer>
1010
<LangVersion>latest</LangVersion>
1111
</PropertyGroup>
1212

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
os: Visual Studio 2019
1+
os: Visual Studio 2022
22
build: off
33

44
environment:
55
CONFIGURATION: Release
66

77
install:
88
- set PATH=C:\Ruby25\bin;%PATH%
9-
- bundle install
9+
- bundle install
1010

1111
before_test:
1212
- dotnet --info

src/NSubstitute/NSubstitute.csproj

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>NSubstitute is a friendly substitute for .NET mocking libraries. It has a simple, succinct syntax to help developers write clearer tests. NSubstitute is designed for Arrange-Act-Assert (AAA) testing and with Test Driven Development (TDD) in mind.</Description>
@@ -16,9 +16,19 @@
1616
</ItemGroup>
1717

1818
<PropertyGroup>
19-
<TargetFrameworks>netstandard1.3;netstandard2.0;net45;net46;net5.0</TargetFrameworks>
19+
<TargetFrameworks>netstandard1.3;netstandard2.0;net45;net46;net5.0;net6.0</TargetFrameworks>
2020
</PropertyGroup>
2121

22+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
23+
<PackageReference Include="System.Linq.Queryable" Version="4.3.0-*" />
24+
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.3.0-*" />
25+
</ItemGroup>
26+
27+
<PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
28+
<DefineConstants>$(DefineConstants);SYSTEM_REFLECTION_CUSTOMATTRIBUTES_IS_ARRAY</DefineConstants>
29+
</PropertyGroup>
30+
31+
2232
<PropertyGroup>
2333
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
2434
<WarningsAsErrors />
@@ -34,28 +44,18 @@
3444
</PropertyGroup>
3545

3646
<PropertyGroup>
37-
<Nullable Condition="'$(TargetIsNet5)' == 'true'">enable</Nullable>
47+
<Nullable Condition="'$(TargetIsNet5OrNewer)' == 'true'">enable</Nullable>
3848
<!-- Nullability does not work nicely for older versions of .NET, so just disable nullability for those versions. -->
3949
<!-- CS8632 - The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. -->
40-
<NoWarn Condition="'$(TargetIsNet5)' != 'true'">$(NoWarn);CS8632</NoWarn>
50+
<NoWarn Condition="'$(TargetIsNet5OrNewer)' != 'true'">$(NoWarn);CS8632</NoWarn>
4151
</PropertyGroup>
4252

4353
<ItemGroup>
4454
<PackageReference Include="Castle.Core" Version="4.4.1-*" />
45-
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0-*"
46-
Condition="'$(TargetIsNet5)' != 'true'" />
55+
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0-*" Condition="'$(TargetIsNet5OrNewer)' != 'true'" />
4756
</ItemGroup>
4857

49-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
50-
<PackageReference Include="System.Linq.Queryable" Version="4.3.0-*" />
51-
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.3.0-*" />
52-
</ItemGroup>
53-
54-
<PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
55-
<DefineConstants>$(DefineConstants);SYSTEM_REFLECTION_CUSTOMATTRIBUTES_IS_ARRAY</DefineConstants>
56-
</PropertyGroup>
57-
58-
<PropertyGroup Condition="'$(TargetIsNet5)' == 'true'">
58+
<PropertyGroup Condition="'$(TargetIsNet5OrNewer)' == 'true'">
5959
<DefineConstants>$(DefineConstants);SYSTEM_DIAGNOSTICS_CODEANALYSIS_NULLABILITY</DefineConstants>
6060
</PropertyGroup>
6161

src/NSubstitute/Routing/Handlers/RaiseEventHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public RouteAction Handle(ICall call)
3030
var handlers = _eventHandlerRegistry.GetHandlers(eventInfo.Name);
3131
foreach (Delegate handler in handlers)
3232
{
33+
if (handler == null)
34+
{
35+
continue;
36+
}
37+
3338
try
3439
{
3540
handler.DynamicInvoke(eventArguments);

tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using NSubstitute.Acceptance.Specs.Infrastructure;
34
using NSubstitute.Core;
45
using NSubstitute.Core.Arguments;
@@ -52,6 +53,63 @@ public void Should_not_match_when_arg_matcher_throws()
5253
Assert.That(_something.Say(null), Is.EqualTo(string.Empty));
5354
}
5455

56+
[Test]
57+
public void Should_match_value_types_by_content()
58+
{
59+
const int intToMatch = 123;
60+
const int identicalInt = 123;
61+
_something.Echo(Arg.Is(intToMatch)).Returns("matching int");
62+
63+
Assert.That(_something.Echo(intToMatch), Is.EqualTo("matching int"));
64+
Assert.That(_something.Echo(identicalInt), Is.EqualTo("matching int"));
65+
66+
var dateToMatch = new DateTime(2021, 10, 22);
67+
var identicalDate = new DateTime(2021, 10, 22);
68+
_something.Anything(dateToMatch).Returns(20211022);
69+
70+
Assert.That(_something.Anything(dateToMatch), Is.EqualTo(20211022));
71+
Assert.That(_something.Anything(identicalDate), Is.EqualTo(20211022));
72+
}
73+
74+
[Test]
75+
public void Should_match_strings_by_content()
76+
{
77+
const string stringToMatch = "hello";
78+
_something.Say(Arg.Is(stringToMatch)).Returns("hi");
79+
80+
Assert.That(_something.Say(stringToMatch), Is.EqualTo("hi"));
81+
Assert.That(_something.Say("hello"), Is.EqualTo("hi"));
82+
}
83+
84+
[Test]
85+
public void Should_match_nullable_ref_types_by_content()
86+
{
87+
#nullable enable
88+
SomeClass? nullClassToMatch = null;
89+
List<int>? nullList = null;
90+
_something.Anything(Arg.Is(nullClassToMatch)).Returns(456);
91+
92+
Assert.That(_something.Anything(nullClassToMatch), Is.EqualTo(456));
93+
Assert.That(_something.Anything(nullList), Is.EqualTo(456));
94+
#nullable disable
95+
}
96+
97+
[Test]
98+
public void Should_match_non_string_non_record_ref_types_by_reference()
99+
{
100+
var listToMatch = new List<int>{1, 2};
101+
_something.Anything(Arg.Is(listToMatch)).Returns(123);
102+
103+
Assert.That(_something.Anything(listToMatch), Is.EqualTo(123));
104+
Assert.That(_something.Anything(new List<int>{1, 2}), Is.EqualTo(0));
105+
106+
var classToMatch = new SomeClass();
107+
_something.Anything(Arg.Is(classToMatch)).Returns(456);
108+
109+
Assert.That(_something.Anything(classToMatch), Is.EqualTo(456));
110+
Assert.That(_something.Anything(new SomeClass()), Is.EqualTo(0));
111+
}
112+
55113
[Test]
56114
public void Return_result_with_only_one_matcher_for_that_type()
57115
{
@@ -233,10 +291,10 @@ public void Should_allow_to_check_received_using_properties_from_other_substitut
233291
// Arrange
234292
var otherSubs = Substitute.For<ISomething>();
235293
otherSubs.SomeProperty.Returns(42);
236-
294+
237295
// Act
238296
_something.Echo(42);
239-
297+
240298
// Assert
241299
_something.Received().Echo(otherSubs.SomeProperty);
242300
}

tests/NSubstitute.Acceptance.Specs/EventChecking.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,35 @@ public void Check_if_event_was_subscribed_to()
1717
Assert.Throws<ReceivedCallsException>(() => engine.Received().Started += someOtherHandler);
1818
}
1919

20+
[Test]
21+
public void Check_if_nullHandlers_are_ignored()
22+
{
23+
var raised = false;
24+
var source = Substitute.For<IEngine>();
25+
source.Started += null;
26+
source.Started += () => raised = true;
27+
source.Started += Raise.Event<Action>();
28+
29+
Assert.IsTrue(raised);
30+
}
31+
32+
[Test]
33+
public void Check_if_multiple_handlers_get_called()
34+
{
35+
var raised1 = false;
36+
var raised2 = false;
37+
var raised3 = false;
38+
var source = Substitute.For<IEngine>();
39+
source.Started += () => raised1 = true;
40+
source.Started += () => raised2 = true;
41+
source.Started += () => raised3 = true;
42+
source.Started += Raise.Event<Action>();
43+
44+
Assert.IsTrue(raised1, "The first handler was not called");
45+
Assert.IsTrue(raised2, "The second handler was not called");
46+
Assert.IsTrue(raised3, "The third handler was not called");
47+
}
48+
2049
public interface IEngine
2150
{
2251
event Action Started;

tests/NSubstitute.Acceptance.Specs/NSubstitute.Acceptance.Specs.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp2.0;netcoreapp1.1;net46;net45;net5.0</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp2.1;net46;net45;net5.0;net6.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
9-
<PackageReference Include="NUnit" Version="3.10.1" />
10-
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
9+
<PackageReference Include="NUnit" Version="3.13.2" />
10+
<PackageReference Include="NUnit3TestAdapter" Version="4.1.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup Condition="'$(TargetIsNetFx)' == 'true'">

tests/NSubstitute.Benchmarks/ActivationBenchmark.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BenchmarkDotNet.Attributes;
2-
using BenchmarkDotNet.Attributes.Jobs;
32
using NSubstitute.Benchmarks.TestTypes;
43

54
namespace NSubstitute.Benchmarks

0 commit comments

Comments
 (0)