Skip to content

Commit 8ecef41

Browse files
authored
add NSubstitute and remove Moq (#39)
1 parent 8601f65 commit 8ecef41

18 files changed

+428
-433
lines changed

Directory.Build.props

+6
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@
3030
<PublicKey>00240000048000009400000006020000002400005253413100040000010001003d5c022c088a46d41d5a5bf7591f3a3dcba30f76b0f43a312b6e45bb419d32283175cbd8bfd83134b123da6db83479e50596fb6bbe0e8c6cef50c01c64a0861c963daaf6905920f44ffe1ce44b3cfcb9c23779f34bc90c7b04e74e36a19bb58af3a69456d49b56993969dba9f8e9e935c2757844a11066d1091477f10cd923b7</PublicKey>
3131
</PropertyGroup>
3232

33+
<!-- Block Projects with Privacy/Security Concerns -->
34+
<Target Name="CheckBlockedPackages" AfterTargets="ResolvePackageDependenciesForBuild">
35+
<Error Code="420" Text="Blocked package dependency detected: %(PackageDependencies.Identity)"
36+
Condition="'%(PackageDependencies.Identity)' == 'Devlooped.SponsorLink'" />
37+
</Target>
38+
3339
</Project>

tests/MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests/DependencyInjection/HttpUserAgentParserServiceCollectionExtensionsTests.cs

+12-13
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66
using MyCSharp.HttpUserAgentParser.DependencyInjection;
77
using Xunit;
88

9-
namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests.DependencyInjection
9+
namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests.DependencyInjection;
10+
11+
public class HttpUserAgentParserDependencyInjectionOptionsExtensionsTests
1012
{
11-
public class HttpUserAgentParserDependencyInjectionOptionsExtensionsTests
13+
[Fact]
14+
public void AddHttpUserAgentParserAccessor()
1215
{
13-
[Fact]
14-
public void AddHttpUserAgentParserAccessor()
15-
{
16-
ServiceCollection services = new();
17-
HttpUserAgentParserDependencyInjectionOptions options = new(services);
16+
ServiceCollection services = new();
17+
HttpUserAgentParserDependencyInjectionOptions options = new(services);
1818

19-
options.AddHttpUserAgentParserAccessor();
19+
options.AddHttpUserAgentParserAccessor();
2020

21-
services.Count.Should().Be(1);
21+
services.Count.Should().Be(1);
2222

23-
services[0].ServiceType.Should().Be<IHttpUserAgentParserAccessor>();
24-
services[0].ImplementationType.Should().Be<HttpUserAgentParserAccessor>();
25-
services[0].Lifetime.Should().Be(ServiceLifetime.Singleton);
26-
}
23+
services[0].ServiceType.Should().Be<IHttpUserAgentParserAccessor>();
24+
services[0].ImplementationType.Should().Be<HttpUserAgentParserAccessor>();
25+
services[0].Lifetime.Should().Be(ServiceLifetime.Singleton);
2726
}
2827
}

tests/MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests/HttpUserAgentParserAccessorTests.cs

+25-23
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,36 @@
22

33
using FluentAssertions;
44
using Microsoft.AspNetCore.Http;
5-
using Moq;
65
using MyCSharp.HttpUserAgentParser.Providers;
76
using MyCSharp.HttpUserAgentParser.TestHelpers;
7+
using NSubstitute;
88
using Xunit;
99

10-
namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests
10+
namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests;
11+
12+
public class HttpUserAgentParserAccessorTests
1113
{
12-
public class HttpUserAgentParserAccessorTests
14+
private readonly IHttpUserAgentParserProvider _parserMock = Substitute.For<IHttpUserAgentParserProvider>();
15+
16+
[Theory]
17+
[InlineData("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62")]
18+
public void Get(string userAgent)
1319
{
14-
[Theory]
15-
[InlineData("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62")]
16-
public void Get(string userAgent)
17-
{
18-
HttpUserAgentInformation userAgentInformation = HttpUserAgentInformation.Parse(userAgent);
19-
20-
Mock<IHttpUserAgentParserProvider> parserMock = new();
21-
{
22-
parserMock.Setup(x => x.Parse(userAgent)).Returns(userAgentInformation);
23-
}
24-
25-
HttpContext httpContext = HttpContextTestHelpers.GetHttpContext(userAgent);
26-
27-
HttpUserAgentParserAccessor accessor = new HttpUserAgentParserAccessor(parserMock.Object);
28-
HttpUserAgentInformation? info = accessor.Get(httpContext);
29-
30-
info.Should().NotBeNull();
31-
info.Should().Be(userAgentInformation);
32-
parserMock.Verify(x => x.Parse(userAgent), Times.Once);
33-
}
20+
// arrange
21+
HttpUserAgentInformation userAgentInformation = HttpUserAgentInformation.Parse(userAgent);
22+
_parserMock.Parse(userAgent).Returns(userAgentInformation);
23+
24+
// act
25+
HttpContext httpContext = HttpContextTestHelpers.GetHttpContext(userAgent);
26+
27+
HttpUserAgentParserAccessor accessor = new(_parserMock);
28+
HttpUserAgentInformation? info = accessor.Get(httpContext);
29+
30+
// assert
31+
info.Should().NotBeNull();
32+
info.Should().Be(userAgentInformation);
33+
34+
// verify
35+
_parserMock.Received(1).Parse(userAgent);
3436
}
3537
}

tests/MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests/MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageReference Include="FluentAssertions" Version="6.7.0" />
1010
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
12-
<PackageReference Include="Moq" Version="4.18.2" />
12+
<PackageReference Include="NSubstitute" Version="5.0.0" />
1313
<PackageReference Include="xunit" Version="2.4.2" />
1414
<PackageReference Include="xunit.runner.console" Version="2.4.2">
1515
<PrivateAssets>all</PrivateAssets>

tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/DependencyInjection/HttpUserAgentParserMemoryCacheServiceCollectionExtensionssTests.cs

+13-14
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,24 @@
66
using MyCSharp.HttpUserAgentParser.Providers;
77
using Xunit;
88

9-
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests.DependencyInjection
9+
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests.DependencyInjection;
10+
11+
public class HttpUserAgentParserMemoryCacheServiceCollectionExtensionssTests
1012
{
11-
public class HttpUserAgentParserMemoryCacheServiceCollectionExtensionssTests
13+
[Fact]
14+
public void AddHttpUserAgentMemoryCachedParser()
1215
{
13-
[Fact]
14-
public void AddHttpUserAgentMemoryCachedParser()
15-
{
16-
ServiceCollection services = new();
16+
ServiceCollection services = new();
1717

18-
services.AddHttpUserAgentMemoryCachedParser();
18+
services.AddHttpUserAgentMemoryCachedParser();
1919

20-
services.Count.Should().Be(2);
20+
services.Count.Should().Be(2);
2121

22-
services[0].ImplementationInstance.Should().BeOfType<HttpUserAgentParserMemoryCachedProviderOptions>();
23-
services[0].Lifetime.Should().Be(ServiceLifetime.Singleton);
22+
services[0].ImplementationInstance.Should().BeOfType<HttpUserAgentParserMemoryCachedProviderOptions>();
23+
services[0].Lifetime.Should().Be(ServiceLifetime.Singleton);
2424

25-
services[1].ServiceType.Should().Be<IHttpUserAgentParserProvider>();
26-
services[1].ImplementationType.Should().Be<HttpUserAgentParserMemoryCachedProvider>();
27-
services[1].Lifetime.Should().Be(ServiceLifetime.Singleton);
28-
}
25+
services[1].ServiceType.Should().Be<IHttpUserAgentParserProvider>();
26+
services[1].ImplementationType.Should().Be<HttpUserAgentParserMemoryCachedProvider>();
27+
services[1].Lifetime.Should().Be(ServiceLifetime.Singleton);
2928
}
3029
}

tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/HttpUserAgentParserMemoryCachedProviderOptionsTests.cs

+33-34
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,50 @@
44
using Microsoft.Extensions.Caching.Memory;
55
using Xunit;
66

7-
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests
7+
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests;
8+
9+
public class HttpUserAgentParserMemoryCachedProviderOptionsTests
810
{
9-
public class HttpUserAgentParserMemoryCachedProviderOptionsTests
11+
[Fact]
12+
public void Ctor()
1013
{
11-
[Fact]
12-
public void Ctor()
13-
{
14-
MemoryCacheOptions cacheOptions = new();
15-
MemoryCacheEntryOptions cacheEntryOptions = new();
14+
MemoryCacheOptions cacheOptions = new();
15+
MemoryCacheEntryOptions cacheEntryOptions = new();
1616

17-
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheOptions, cacheEntryOptions);
17+
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheOptions, cacheEntryOptions);
1818

19-
options.CacheOptions.Should().Be(cacheOptions);
20-
options.CacheEntryOptions.Should().Be(cacheEntryOptions);
21-
}
19+
options.CacheOptions.Should().Be(cacheOptions);
20+
options.CacheEntryOptions.Should().Be(cacheEntryOptions);
21+
}
2222

23-
[Fact]
24-
public void Ctor_MemoryCacheOptions()
25-
{
26-
MemoryCacheOptions cacheOptions = new();
23+
[Fact]
24+
public void Ctor_MemoryCacheOptions()
25+
{
26+
MemoryCacheOptions cacheOptions = new();
2727

28-
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheOptions);
28+
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheOptions);
2929

30-
options.CacheOptions.Should().Be(cacheOptions);
31-
options.CacheEntryOptions.Should().NotBeNull();
32-
}
30+
options.CacheOptions.Should().Be(cacheOptions);
31+
options.CacheEntryOptions.Should().NotBeNull();
32+
}
3333

34-
[Fact]
35-
public void Ctor_MemoryCacheEntryOptions()
36-
{
37-
MemoryCacheEntryOptions cacheEntryOptions = new();
34+
[Fact]
35+
public void Ctor_MemoryCacheEntryOptions()
36+
{
37+
MemoryCacheEntryOptions cacheEntryOptions = new();
3838

39-
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheEntryOptions);
39+
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheEntryOptions);
4040

41-
options.CacheOptions.Should().NotBeNull();
42-
options.CacheEntryOptions.Should().Be(cacheEntryOptions);
43-
}
41+
options.CacheOptions.Should().NotBeNull();
42+
options.CacheEntryOptions.Should().Be(cacheEntryOptions);
43+
}
4444

45-
[Fact]
46-
public void Ctor_Empty()
47-
{
48-
HttpUserAgentParserMemoryCachedProviderOptions options = new();
45+
[Fact]
46+
public void Ctor_Empty()
47+
{
48+
HttpUserAgentParserMemoryCachedProviderOptions options = new();
4949

50-
options.CacheOptions.Should().NotBeNull();
51-
options.CacheEntryOptions.Should().NotBeNull();
52-
}
50+
options.CacheOptions.Should().NotBeNull();
51+
options.CacheEntryOptions.Should().NotBeNull();
5352
}
5453
}

tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/HttpUserAgentParserMemoryCachedProviderTests.cs

+22-23
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,39 @@
33
using FluentAssertions;
44
using Xunit;
55

6-
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests
6+
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests;
7+
8+
public class HttpUserAgentParserMemoryCachedProviderTests
79
{
8-
public class HttpUserAgentParserMemoryCachedProviderTests
10+
[Fact]
11+
public void Parse()
912
{
10-
[Fact]
11-
public void Parse()
12-
{
13-
HttpUserAgentParserMemoryCachedProviderOptions cachedProviderOptions = new();
14-
HttpUserAgentParserMemoryCachedProvider provider = new(cachedProviderOptions);
13+
HttpUserAgentParserMemoryCachedProviderOptions cachedProviderOptions = new();
14+
HttpUserAgentParserMemoryCachedProvider provider = new(cachedProviderOptions);
1515

16-
// create first
17-
string userAgentOne =
18-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62";
16+
// create first
17+
string userAgentOne =
18+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62";
1919

20-
HttpUserAgentInformation infoOne = provider.Parse(userAgentOne);
20+
HttpUserAgentInformation infoOne = provider.Parse(userAgentOne);
2121

22-
infoOne.Name.Should().Be("Edge");
23-
infoOne.Version.Should().Be("90.0.818.62");
22+
infoOne.Name.Should().Be("Edge");
23+
infoOne.Version.Should().Be("90.0.818.62");
2424

25-
// check duplicate
25+
// check duplicate
2626

27-
HttpUserAgentInformation infoDuplicate = provider.Parse(userAgentOne);
27+
HttpUserAgentInformation infoDuplicate = provider.Parse(userAgentOne);
2828

29-
infoDuplicate.Name.Should().Be("Edge");
30-
infoDuplicate.Version.Should().Be("90.0.818.62");
29+
infoDuplicate.Name.Should().Be("Edge");
30+
infoDuplicate.Version.Should().Be("90.0.818.62");
3131

32-
// create second
32+
// create second
3333

34-
string userAgentTwo = "Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0";
34+
string userAgentTwo = "Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0";
3535

36-
HttpUserAgentInformation infoTwo = provider.Parse(userAgentTwo);
36+
HttpUserAgentInformation infoTwo = provider.Parse(userAgentTwo);
3737

38-
infoTwo.Name.Should().Be("Firefox");
39-
infoTwo.Version.Should().Be("41.0");
40-
}
38+
infoTwo.Name.Should().Be("Firefox");
39+
infoTwo.Version.Should().Be("41.0");
4140
}
4241
}

tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
1111
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
13-
<PackageReference Include="Moq" Version="4.18.2" />
1413
<PackageReference Include="xunit" Version="2.4.2" />
1514
<PackageReference Include="xunit.runner.console" Version="2.4.2">
1615
<PrivateAssets>all</PrivateAssets>

tests/MyCSharp.HttpUserAgentParser.TestHelpers/HttpContextTestHelpers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class HttpContextTestHelpers
88
{
99
public static HttpContext GetHttpContext(string userAgent)
1010
{
11-
DefaultHttpContext context = new DefaultHttpContext();
11+
DefaultHttpContext context = new();
1212
context.Request.Headers["User-Agent"] = userAgent;
1313

1414
return context;

tests/MyCSharp.HttpUserAgentParser.UnitTests/DependencyInjection/HttpUserAgentParserDependencyInjectionOptions.cs

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
using FluentAssertions;
44
using Microsoft.Extensions.DependencyInjection;
5-
using Moq;
65
using MyCSharp.HttpUserAgentParser.DependencyInjection;
6+
using NSubstitute;
77
using Xunit;
88

9-
namespace MyCSharp.HttpUserAgentParser.UnitTests.DependencyInjection
9+
namespace MyCSharp.HttpUserAgentParser.UnitTests.DependencyInjection;
10+
11+
public class UserAgentParserDependencyInjectionOptionsTests
1012
{
11-
public class UserAgentParserDependencyInjectionOptionsTests
12-
{
13-
[Fact]
14-
public void Ctor_Should_Set_Property()
15-
{
16-
Mock<IServiceCollection> scMock = new();
13+
private readonly IServiceCollection scMock = Substitute.For<IServiceCollection>();
1714

18-
HttpUserAgentParserDependencyInjectionOptions options = new(scMock.Object);
15+
[Fact]
16+
public void Ctor_Should_Set_Property()
17+
{
18+
HttpUserAgentParserDependencyInjectionOptions options = new(scMock);
1919

20-
options.Services.Should().BeEquivalentTo(scMock.Object);
21-
}
20+
options.Services.Should().BeEquivalentTo(scMock);
2221
}
2322
}

0 commit comments

Comments
 (0)