Skip to content

Commit

Permalink
add NSubstitute and remove Moq
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminAbt committed Aug 9, 2023
1 parent 8601f65 commit 223b31f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@

using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Moq;
using MyCSharp.HttpUserAgentParser.Providers;
using MyCSharp.HttpUserAgentParser.TestHelpers;
using NSubstitute;
using Xunit;

namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests
{
public class HttpUserAgentParserAccessorTests
{
private readonly IHttpUserAgentParserProvider _parserMock = Substitute.For<IHttpUserAgentParserProvider>();

[Theory]
[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")]
public void Get(string userAgent)
{
// arrange
HttpUserAgentInformation userAgentInformation = HttpUserAgentInformation.Parse(userAgent);
_parserMock.Parse(userAgent).Returns(userAgentInformation);

Mock<IHttpUserAgentParserProvider> parserMock = new();
{
parserMock.Setup(x => x.Parse(userAgent)).Returns(userAgentInformation);
}

// act
HttpContext httpContext = HttpContextTestHelpers.GetHttpContext(userAgent);

HttpUserAgentParserAccessor accessor = new HttpUserAgentParserAccessor(parserMock.Object);
HttpUserAgentParserAccessor accessor = new(_parserMock);
HttpUserAgentInformation? info = accessor.Get(httpContext);

// assert
info.Should().NotBeNull();
info.Should().Be(userAgentInformation);
parserMock.Verify(x => x.Parse(userAgent), Times.Once);

// verify
_parserMock.Received(1).Parse(userAgent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.console" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.console" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.console" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 223b31f

Please sign in to comment.