Skip to content

Commit 11c777c

Browse files
authored
Merge pull request #258 from Yubico/dennisdyallo/nsubstitute
Replace Moq with NSubstitute
2 parents 3552293 + 4b4538a commit 11c777c

File tree

14 files changed

+363
-411
lines changed

14 files changed

+363
-411
lines changed

Yubico.Core/tests/Yubico.Core.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ limitations under the License. -->
4343
<DefineConstants>Linux</DefineConstants>
4444
</PropertyGroup>
4545
<ItemGroup>
46+
<PackageReference Include="NSubstitute" Version="5.3.0" />
4647
<ProjectReference Include="..\src\Yubico.Core.csproj" />
4748
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
4849
<PackageReference Include="coverlet.collector" Version="6.0.2" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
4950
<PackageReference Include="xunit" Version="2.8.1" />
5051
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
5152
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
52-
<PackageReference Include="Moq" Version="4.16.1" />
5353

5454
<None Update="appsettings.json">
5555
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

Yubico.Core/tests/Yubico/Core/Devices/Hid/HidConnectionTests.cs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ public class HidConnectionTests
2020
{
2121
private static IHidDDevice GetMockedDevice()
2222
{
23-
var mock = new Mock<IHidDDevice>();
24-
return mock.Object;
23+
var mock = Substitute.For<IHidDDevice>();
24+
return mock;
2525
}
2626

2727
private static byte[] GetFeatureReport() => Hex.HexToBytes("000102030405060708090A0B0C0D0E0F");
2828
private static byte[] GetInputReport() => Hex.HexToBytes("0001020304050607");
2929
private static byte[] GetOutputReport() => Hex.HexToBytes("00010203");
3030

31-
private static Mock<IHidDDevice> GetMockedFeatureDevice()
31+
private static IHidDDevice GetMockedFeatureDevice()
3232
{
33-
var mock = new Mock<IHidDDevice>();
34-
_ = mock.Setup(hdd => hdd.FeatureReportByteLength).Returns(16);
35-
_ = mock.Setup(hdd => hdd.InputReportByteLength).Throws(new Exception());
33+
var mock = Substitute.For<IHidDDevice>();
34+
_ = mock.When(hdd => hdd.FeatureReportByteLength).Returns(16);
35+
_ = mock.InputReportByteLength).Throws(new Exception());
3636
_ = mock.Setup(hdd => hdd.OutputReportByteLength).Throws(new Exception());
37-
_ = mock.Setup(hdd => hdd.GetInputReport()).Throws(new Exception());
38-
_ = mock.Setup(hdd => hdd.GetFeatureReport()).Returns(GetFeatureReport());
37+
_ = mock.Setup(hdd => hdd.GetInputReport()).Throw(new Exception());
38+
_ = mock.When(hdd => hdd.GetFeatureReport().Returns(GetFeatureReport());
3939
return mock;
4040
}
4141

42-
private static Mock<IHidDDevice> GetMockedIODevice()
42+
private static IHidDDevice GetMockedIODevice()
4343
{
44-
var mock = new Mock<IHidDDevice>();
45-
_ = mock.Setup(hdd => hdd.InputReportByteLength).Returns(8);
46-
_ = mock.Setup(hdd => hdd.OutputReportByteLength).Returns(4);
47-
_ = mock.Setup(hdd => hdd.FeatureReportByteLength).Throws(new Exception());
48-
_ = mock.Setup(hdd => hdd.GetFeatureReport()).Throws(new Exception());
49-
_ = mock.Setup(hdd => hdd.GetInputReport()).Returns(GetInputReport());
44+
var mock = Substitute.For<IHidDDevice>();
45+
_ = mock.InputReportByteLength.Returns(8);
46+
_ = mock.OutputReportByteLength.Returns(4);
47+
_ = mock.FeatureReportByteLength).Throws(new Exception());
48+
_ = mock.Setup(hdd => hdd.GetFeatureReport()).Throw(new Exception());
49+
_ = mock.Setup(hdd => hdd.GetInputReport().Returns(GetInputReport());
5050
return mock;
5151
}
5252

@@ -59,34 +59,34 @@ public void Constructor_GivenDevice_Succeeds()
5959
[Fact]
6060
public void Constructor_GivenFeatureDevice_CallsOpenFeatureConnection()
6161
{
62-
Mock<IHidDDevice> mock = GetMockedFeatureDevice();
63-
using var hc = new HidFeatureReportConnection(mock.Object);
64-
mock.Verify(hdd => hdd.OpenFeatureConnection(), Times.Once());
62+
IHidDDevice mock = GetMockedFeatureDevice();
63+
using var hc = new HidFeatureReportConnection(mock);
64+
mock.Received().OpenFeatureConnection();
6565
}
6666

6767
[Fact]
6868
public void Constructor_GivenFeatureDevice_SetsInputReportSize()
6969
{
70-
Mock<IHidDDevice> mock = GetMockedFeatureDevice();
71-
using var hc = new HidFeatureReportConnection(mock.Object);
70+
IHidDDevice mock = GetMockedFeatureDevice();
71+
using var hc = new HidFeatureReportConnection(mock);
7272

73-
Assert.Equal(hc.InputReportSize, mock.Object.FeatureReportByteLength);
73+
Assert.Equal(hc.InputReportSize, mock.FeatureReportByteLength);
7474
}
7575

7676
[Fact]
7777
public void Constructor_GivenFeatureDevice_SetsOutputReportSize()
7878
{
79-
Mock<IHidDDevice> mock = GetMockedFeatureDevice();
80-
using var hc = new HidFeatureReportConnection(mock.Object);
79+
IHidDDevice mock = GetMockedFeatureDevice();
80+
using var hc = new HidFeatureReportConnection(mock);
8181

82-
Assert.Equal(hc.OutputReportSize, mock.Object.FeatureReportByteLength);
82+
Assert.Equal(hc.OutputReportSize, mock.FeatureReportByteLength);
8383
}
8484

8585
[Fact]
8686
public void SetReport_GivenNullReport_ThrowsArgumentNullException()
8787
{
88-
Mock<IHidDDevice> mock = GetMockedFeatureDevice();
89-
using var hc = new HidFeatureReportConnection(mock.Object);
88+
IHidDDevice mock = GetMockedFeatureDevice();
89+
using var hc = new HidFeatureReportConnection(mock);
9090

9191
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
9292
_ = Assert.Throws<ArgumentNullException>(() => hc.SetReport(null));
@@ -96,78 +96,78 @@ public void SetReport_GivenNullReport_ThrowsArgumentNullException()
9696
[Fact]
9797
public void SetReport_GivenFeatureReports_CallsSetFeatureReport()
9898
{
99-
Mock<IHidDDevice> mock = GetMockedFeatureDevice();
100-
using var hc = new HidFeatureReportConnection(mock.Object);
99+
IHidDDevice mock = GetMockedFeatureDevice();
100+
using var hc = new HidFeatureReportConnection(mock);
101101

102102
hc.SetReport(GetFeatureReport());
103103

104-
mock.Verify(hdd => hdd.SetFeatureReport(IsSeqEqual(GetFeatureReport())), Times.Once());
104+
mock.Received().SetFeatureReport(IsSeqEqual(GetFeatureReport()));
105105
}
106106

107107
[Fact]
108108
public void GetReport_GivenFeatureReports_CallsGetFeatureReport()
109109
{
110-
Mock<IHidDDevice> mock = GetMockedFeatureDevice();
111-
using var hc = new HidFeatureReportConnection(mock.Object);
110+
IHidDDevice mock = GetMockedFeatureDevice();
111+
using var hc = new HidFeatureReportConnection(mock);
112112

113113
byte[] report = hc.GetReport();
114114

115-
mock.Verify(hdd => hdd.GetFeatureReport(), Times.Once());
115+
mock.Received().GetFeatureReport();
116116
Assert.Equal(GetFeatureReport(), report);
117117
}
118118

119119
[Fact]
120120
public void Constructor_GivenIODevice_CallsOpenIOConnection()
121121
{
122-
Mock<IHidDDevice> mock = GetMockedIODevice();
123-
using var hc = new HidIOReportConnection(mock.Object);
124-
mock.Verify(hdd => hdd.OpenIOConnection(), Times.Once());
122+
IHidDDevice mock = GetMockedIODevice();
123+
using var hc = new HidIOReportConnection(mock);
124+
mock.Received().OpenIOConnection();
125125
}
126126

127127
[Fact]
128128
public void Constructor_GivenIODevice_SetsInputReportSize()
129129
{
130-
Mock<IHidDDevice> mock = GetMockedIODevice();
131-
using var hc = new HidIOReportConnection(mock.Object);
130+
IHidDDevice mock = GetMockedIODevice();
131+
using var hc = new HidIOReportConnection(mock);
132132

133-
Assert.Equal(hc.InputReportSize, mock.Object.InputReportByteLength);
133+
Assert.Equal(hc.InputReportSize, mock.InputReportByteLength);
134134
}
135135

136136
[Fact]
137137
public void Constructor_GivenIODevice_SetsOutputReportSize()
138138
{
139-
Mock<IHidDDevice> mock = GetMockedIODevice();
140-
using var hc = new HidIOReportConnection(mock.Object);
139+
IHidDDevice mock = GetMockedIODevice();
140+
using var hc = new HidIOReportConnection(mock);
141141

142-
Assert.Equal(hc.OutputReportSize, mock.Object.OutputReportByteLength);
142+
Assert.Equal(hc.OutputReportSize, mock.OutputReportByteLength);
143143
}
144144

145145
[Fact]
146146
public void SetReport_GivenIOReports_CallsSetOutputReport()
147147
{
148-
Mock<IHidDDevice> mock = GetMockedIODevice();
149-
using var hc = new HidIOReportConnection(mock.Object);
148+
IHidDDevice mock = GetMockedIODevice();
149+
using var hc = new HidIOReportConnection(mock);
150150

151151
hc.SetReport(GetOutputReport());
152152

153-
mock.Verify(hdd => hdd.SetOutputReport(IsSeqEqual(GetOutputReport())), Times.Once());
153+
mock.Received().SetOutputReport(IsSeqEqual(GetOutputReport()));
154154
}
155155

156156
[Fact]
157157
public void GetReport_GivenIOReports_CallsGetInputReport()
158158
{
159-
Mock<IHidDDevice> mock = GetMockedIODevice();
160-
using var hc = new HidIOReportConnection(mock.Object);
159+
IHidDDevice mock = GetMockedIODevice();
160+
using var hc = new HidIOReportConnection(mock);
161161

162162
byte[] report = hc.GetReport();
163163

164-
mock.Verify(hdd => hdd.GetInputReport(), Times.Once());
164+
mock.Received().GetInputReport();
165165
Assert.Equal(GetInputReport(), report);
166166
}
167167

168168
private static byte[] IsSeqEqual(byte[] val)
169169
{
170-
return It.Is<byte[]>(b => b.SequenceEqual(val));
170+
return Arg.Is<byte[]>(b => b.SequenceEqual(val));
171171
}
172172
}
173173
#endif

Yubico.Core/tests/Yubico/Core/Logging/LogTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
using System;
1616
using Microsoft.Extensions.Logging;
17-
using Moq;
17+
using NSubstitute;
1818
using Xunit;
1919

2020
namespace Yubico.Core.Logging
@@ -49,14 +49,14 @@ public void DefaultLoggerFactory_IsCreated_WhenNoConfigurationProvided()
4949
public void ManualLoggerFactory_SettingInstance_OverridesDefaultFactory()
5050
{
5151
// Arrange
52-
var mockLoggerFactory = new Mock<ILoggerFactory>();
53-
Log.Instance = mockLoggerFactory.Object;
52+
var mockLoggerFactory = Substitute.For<ILoggerFactory>();
53+
Log.Instance = mockLoggerFactory;
5454

5555
// Act
5656
ILoggerFactory actualFactory = Log.Instance;
5757

5858
// Assert
59-
Assert.Same(mockLoggerFactory.Object, actualFactory);
59+
Assert.Same(mockLoggerFactory, actualFactory);
6060
}
6161

6262
// Ensure that LoggerFactory can be replaced manually using the Instance property.
@@ -65,16 +65,16 @@ public void ManualLoggerFactory_SettingInstance_OverridesDefaultFactory()
6565
public void Legacy_ManualLoggerFactory_SettingInstance_OverridesDefaultFactory()
6666
{
6767
// Arrange
68-
var mockLoggerFactory = new Mock<ILoggerFactory>();
68+
var mockLoggerFactory = Substitute.For<ILoggerFactory>();
6969
#pragma warning disable CS0618 // Type or member is obsolete
70-
Log.LoggerFactory = mockLoggerFactory.Object;
70+
Log.LoggerFactory = mockLoggerFactory;
7171
#pragma warning restore CS0618 // Type or member is obsolete
7272

7373
// Act
7474
ILoggerFactory actualFactory = Log.Instance;
7575

7676
// Assert
77-
Assert.Same(mockLoggerFactory.Object, actualFactory);
77+
Assert.Same(mockLoggerFactory, actualFactory);
7878
}
7979
}
8080
}

Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/ScpApduTransform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void InitializeScp11(Scp11KeyParameters keyParameters)
129129
}
130130

131131
[DoesNotReturn]
132-
private T ThrowIfUninitialized<T>() => throw new InvalidOperationException($"{nameof(Scp.ScpState)} has not been initialized. The Setup method must be called.");
132+
private static T ThrowIfUninitialized<T>() => throw new InvalidOperationException($"{nameof(Scp.ScpState)} has not been initialized. The Setup method must be called.");
133133

134134
private static bool ShouldNotEncode(Type commandType)
135135
{
Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
<!-- Copyright 2021 Yubico AB
2-
3-
Licensed under the Apache License, Version 2.0 (the "License");
4-
you may not use this file except in compliance with the License.
5-
You may obtain a copy of the License at
6-
7-
http://www.apache.org/licenses/LICENSE-2.0
8-
9-
Unless required by applicable law or agreed to in writing, software
10-
distributed under the License is distributed on an "AS IS" BASIS,
11-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
See the License for the specific language governing permissions and
13-
limitations under the License. -->
14-
15-
<Project Sdk="Microsoft.NET.Sdk">
16-
17-
<PropertyGroup>
18-
<AssemblyName>Yubico.YubiKey.UnitTests</AssemblyName>
19-
<PackageId>Yubico.YubiKey.UnitTests</PackageId>
20-
<RootNamespace></RootNamespace>
21-
22-
<TargetFramework>net8.0</TargetFramework>
23-
24-
<AnalysisMode>AllDisabledByDefault</AnalysisMode>
25-
26-
<!-- StrongName signing -->
27-
<!-- StrongNaming requires that friend assemblies are strong named as well. That means this unit test project must
28-
be strong named, since it uses InternalsVisibleTo. -->
29-
<SignAssembly>true</SignAssembly>
30-
<AssemblyOriginatorKeyFile>..\..\..\Yubico.NET.SDK.snk</AssemblyOriginatorKeyFile>
31-
</PropertyGroup>
32-
33-
<ItemGroup>
34-
<ProjectReference Include="..\..\src\Yubico.YubiKey.csproj" />
35-
<ProjectReference Include="..\utilities\Yubico.YubiKey.TestUtilities.csproj" />
36-
<PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
37-
<PackageReference Include="xunit" Version="2.9.3" />
38-
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
39-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
40-
<PackageReference Include="Moq" Version="4.20.72" />
41-
</ItemGroup>
42-
43-
<ItemGroup>
44-
<None Update="appsettings.json">
45-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
46-
</None>
47-
</ItemGroup>
48-
</Project>
1+
<!-- Copyright 2021 Yubico AB
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. -->
14+
15+
<Project Sdk="Microsoft.NET.Sdk">
16+
17+
<PropertyGroup>
18+
<AssemblyName>Yubico.YubiKey.UnitTests</AssemblyName>
19+
<PackageId>Yubico.YubiKey.UnitTests</PackageId>
20+
<RootNamespace></RootNamespace>
21+
22+
<TargetFramework>net8.0</TargetFramework>
23+
24+
<AnalysisMode>AllDisabledByDefault</AnalysisMode>
25+
26+
<!-- StrongName signing -->
27+
<!-- StrongNaming requires that friend assemblies are strong named as well. That means this unit test project must
28+
be strong named, since it uses InternalsVisibleTo. -->
29+
<SignAssembly>true</SignAssembly>
30+
<AssemblyOriginatorKeyFile>..\..\..\Yubico.NET.SDK.snk</AssemblyOriginatorKeyFile>
31+
</PropertyGroup>
32+
33+
<ItemGroup>
34+
<ProjectReference Include="..\..\src\Yubico.YubiKey.csproj" />
35+
<ProjectReference Include="..\utilities\Yubico.YubiKey.TestUtilities.csproj" />
36+
<PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
37+
<PackageReference Include="xunit" Version="2.9.3" />
38+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
39+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
40+
<PackageReference Include="NSubstitute" Version="5.3.0" />
41+
</ItemGroup>
42+
43+
<ItemGroup>
44+
<None Update="appsettings.json">
45+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
46+
</None>
47+
</ItemGroup>
48+
</Project>

0 commit comments

Comments
 (0)