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

GitAuto: [FEATURE] Add Integration Tests project with WireMock fixture #406

Closed
wants to merge 9 commits into from
29 changes: 29 additions & 0 deletions Tests/VTEX.Integration.Tests/ErpServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading.Tasks;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using Xunit;

public class ErpServiceTests : IClassFixture<WireMockServerFixture>
{
private readonly WireMockServerFixture _fixture;

public ErpServiceTests(WireMockServerFixture fixture)
{
_fixture = fixture;
}

[Fact]
public async Task ShouldHandleSuccessfulResponse()
{
// Arrange
_fixture.Server
.Given(Request.Create().WithPath("/api/orders").UsingGet())
.RespondWith(Response.Create().WithStatusCode(200).WithBody("{ \"orderId\": \"12345\" }"));

// Act
var result = await _service.GetOrderAsync("12345");

// Assert
Assert.Equal("12345", result.OrderId);
}
}
21 changes: 21 additions & 0 deletions Tests/VTEX.Integration.Tests/OrderGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Bogus;

public class OrderGenerator

Check warning on line 3 in Tests/VTEX.Integration.Tests/OrderGenerator.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/VTEX.Integration.Tests/OrderGenerator.cs#L3

Add a 'protected' constructor or the 'static' keyword to the class declaration.
{
public static Order CreateFakeOrder()
{
var faker = new Faker<Order>()
.RuleFor(o => o.OrderId, f => f.Random.Guid().ToString())
.RuleFor(o => o.CustomerName, f => f.Name.FullName())
.RuleFor(o => o.Amount, f => f.Finance.Amount());

return faker.Generate();
}
}

public class Order

Check notice on line 16 in Tests/VTEX.Integration.Tests/OrderGenerator.cs

View check run for this annotation

codefactor.io / CodeFactor

Tests/VTEX.Integration.Tests/OrderGenerator.cs#L16

File may only contain a single type. (SA1402)
{
public string OrderId { get; set; }
public string CustomerName { get; set; }
public decimal Amount { get; set; }
}
29 changes: 29 additions & 0 deletions Tests/VTEX.Integration.Tests/OrderServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading.Tasks;
using NSubstitute;
using Xunit;

public class OrderServiceTests
{
private readonly IOrderRepository _orderRepository;
private readonly OrderService _orderService;

public OrderServiceTests()
{
_orderRepository = Substitute.For<IOrderRepository>();
_orderService = new OrderService(_orderRepository);
}

[Fact]
public async Task ShouldReturnOrder()
{
// Arrange
var fakeOrder = OrderGenerator.CreateFakeOrder();
_orderRepository.GetOrderAsync(Arg.Any<string>()).Returns(Task.FromResult(fakeOrder));

// Act
var result = await _orderService.GetOrderAsync(fakeOrder.OrderId);

// Assert
Assert.Equal(fakeOrder.OrderId, result.OrderId);
}
}
19 changes: 19 additions & 0 deletions Tests/VTEX.Integration.Tests/SnapshotTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Threading.Tasks;
using Snapshooter.Xunit;
using Xunit;

public class SnapshotTests
{
[Fact]
public async Task ShouldMatchExpectedResponse()
{
// Arrange
var expectedResponse = new { OrderId = "12345" };

Check notice on line 11 in Tests/VTEX.Integration.Tests/SnapshotTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/VTEX.Integration.Tests/SnapshotTests.cs#L11

Remove the unused local variable 'expectedResponse'.

// Act
var actualResponse = await _service.GetOrderAsync("12345");

// Assert
actualResponse.Should().MatchSnapshot();
}
}
15 changes: 15 additions & 0 deletions Tests/VTEX.Integration.Tests/VTEX.Integration.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="WireMock.Net" Version="3.1.0" />
<PackageReference Include="Snapshooter" Version="1.0.0" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="Bogus" Version="33.1.0" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions Tests/VTEX.Integration.Tests/WireMockServerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using WireMock.Server;
using WireMock.Settings;

public class WireMockServerFixture : IDisposable
{
public WireMockServer Server { get; }

public WireMockServerFixture()
{
Server = WireMockServer.Start(new WireMockServerSettings
{
Urls = new[] { "http://localhost:9091" },
StartAdminInterface = true,
ReadStaticMappings = true
});
}

public void Dispose() => Server.Stop();
}
Loading