Skip to content

Commit 7bdca0a

Browse files
optimised the ui integration tests
1 parent 89de554 commit 7bdca0a

4 files changed

Lines changed: 140 additions & 243 deletions

File tree

ECommerce.AdminUI.IntegrationTests/CustomerServiceIntegrationTests.cs

Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,23 @@
11
using System;
22
using System.Net;
3-
using System.Net.Http;
4-
using System.Text;
5-
using System.Threading;
63
using System.Threading.Tasks;
74
using ECommerce.AdminUI.Services;
8-
using Microsoft.AspNetCore.Http;
95
using Microsoft.Extensions.DependencyInjection;
10-
using Moq;
11-
using Moq.Protected;
126
using Xunit;
137
using Xunit.Abstractions;
148

159
namespace ECommerce.AdminUI.IntegrationTests;
1610

17-
public class CustomerServiceIntegrationTests
11+
public class CustomerServiceIntegrationTests : ServiceIntegrationTestBase
1812
{
19-
private readonly ITestOutputHelper _testOutputHelper;
20-
2113
public CustomerServiceIntegrationTests(ITestOutputHelper testOutputHelper)
14+
: base(testOutputHelper)
2215
{
23-
_testOutputHelper = testOutputHelper;
24-
}
25-
26-
private HttpClient CreateMockHttpClient(HttpStatusCode statusCode, string content)
27-
{
28-
var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
29-
handlerMock
30-
.Protected()
31-
.Setup<Task<HttpResponseMessage>>(
32-
"SendAsync",
33-
ItExpr.IsAny<HttpRequestMessage>(),
34-
ItExpr.IsAny<CancellationToken>())
35-
.ReturnsAsync(new HttpResponseMessage
36-
{
37-
StatusCode = statusCode,
38-
Content = new StringContent(content),
39-
});
40-
41-
return new HttpClient(handlerMock.Object)
42-
{
43-
BaseAddress = new Uri("https://fake-api/")
44-
};
4516
}
4617

47-
private IServiceCollection SetupCommonServices(HttpClient httpClient, bool setupAuthSuccess = false)
18+
protected override void ConfigureServices(IServiceCollection services)
4819
{
49-
var services = new ServiceCollection();
50-
var httpClientFactoryMock = new Mock<IHttpClientFactory>();
51-
httpClientFactoryMock
52-
.Setup(f => f.CreateClient(It.IsAny<string>()))
53-
.Returns(httpClient);
54-
services.AddSingleton(httpClientFactoryMock.Object);
55-
services.AddLogging();
56-
57-
var sessionMock = new Mock<ISession>();
58-
sessionMock.Setup(x => x.TryGetValue("AuthToken", out It.Ref<byte[]>.IsAny))
59-
.Returns((string key, out byte[] value) => { value = Encoding.UTF8.GetBytes("test-token"); return true; });
60-
sessionMock.Setup(x => x.TryGetValue("Username", out It.Ref<byte[]>.IsAny))
61-
.Returns((string key, out byte[] value) => { value = Encoding.UTF8.GetBytes("testuser"); return true; });
62-
63-
var httpContextMock = new Mock<HttpContext>();
64-
httpContextMock.Setup(x => x.Session).Returns(sessionMock.Object);
65-
66-
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
67-
httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
68-
services.AddSingleton(httpContextAccessorMock.Object);
69-
70-
var orderServiceMock = new Mock<IOrderService>();
71-
services.AddSingleton(orderServiceMock.Object);
72-
var productServiceMock = new Mock<IProductService>();
73-
services.AddSingleton(productServiceMock.Object);
74-
var authServiceMock = new Mock<IAuthService>();
75-
if (setupAuthSuccess)
76-
{
77-
authServiceMock
78-
.Setup(x => x.ExecuteWithTokenRefreshAsync(
79-
It.IsAny<Func<string, Task<HttpResponseMessage>>>(),
80-
It.IsAny<string>(),
81-
It.IsAny<string>(),
82-
It.IsAny<HttpContext>()))
83-
.ReturnsAsync((true, new HttpResponseMessage
84-
{
85-
StatusCode = HttpStatusCode.OK,
86-
Content = new StringContent("true"),
87-
}));
88-
}
89-
services.AddSingleton(authServiceMock.Object);
9020
services.AddTransient<ICustomerService, CustomerService>();
91-
return services;
9221
}
9322

9423
[Fact]
@@ -132,8 +61,8 @@ public async Task CreateCustomerAsync_ReturnsTrue_WhenApiReturnsSuccess()
13261
if (!result)
13362
{
13463
var requestJson = System.Text.Json.JsonSerializer.Serialize(customer);
135-
_testOutputHelper.WriteLine($"Customer payload: {requestJson}");
136-
_testOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
64+
TestOutputHelper.WriteLine($"Customer payload: {requestJson}");
65+
TestOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
13766
}
13867

13968
// Assert
@@ -163,8 +92,8 @@ public async Task UpdateCustomerAsync_ReturnsTrue_WhenApiReturnsSuccess()
16392
if (!result)
16493
{
16594
var requestJson = System.Text.Json.JsonSerializer.Serialize(customer);
166-
_testOutputHelper.WriteLine($"Customer payload: {requestJson}");
167-
_testOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
95+
TestOutputHelper.WriteLine($"Customer payload: {requestJson}");
96+
TestOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
16897
}
16998

17099
// Assert

ECommerce.AdminUI.IntegrationTests/OrderServiceIntegrationTests.cs

Lines changed: 5 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,23 @@
11
using System;
22
using System.Net;
3-
using System.Net.Http;
4-
using System.Text;
5-
using System.Threading;
63
using System.Threading.Tasks;
74
using ECommerce.AdminUI.Services;
8-
using Microsoft.AspNetCore.Http;
95
using Microsoft.Extensions.DependencyInjection;
10-
using Moq;
11-
using Moq.Protected;
126
using Xunit;
137
using Xunit.Abstractions;
148

159
namespace ECommerce.AdminUI.IntegrationTests;
1610

17-
public class OrderServiceIntegrationTests
11+
public class OrderServiceIntegrationTests : ServiceIntegrationTestBase
1812
{
19-
private readonly ITestOutputHelper _testOutputHelper;
20-
2113
public OrderServiceIntegrationTests(ITestOutputHelper testOutputHelper)
14+
: base(testOutputHelper)
2215
{
23-
_testOutputHelper = testOutputHelper;
24-
}
25-
26-
private HttpClient CreateMockHttpClient(HttpStatusCode statusCode, string content)
27-
{
28-
var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
29-
handlerMock
30-
.Protected()
31-
.Setup<Task<HttpResponseMessage>>(
32-
"SendAsync",
33-
ItExpr.IsAny<HttpRequestMessage>(),
34-
ItExpr.IsAny<CancellationToken>())
35-
.ReturnsAsync(new HttpResponseMessage
36-
{
37-
StatusCode = statusCode,
38-
Content = new StringContent(content),
39-
});
40-
41-
return new HttpClient(handlerMock.Object)
42-
{
43-
BaseAddress = new Uri("https://fake-api/")
44-
};
4516
}
4617

47-
private IServiceCollection SetupCommonServices(HttpClient httpClient, bool setupAuthSuccess = false)
18+
protected override void ConfigureServices(IServiceCollection services)
4819
{
49-
var services = new ServiceCollection();
50-
51-
// Setup HttpClientFactory
52-
var httpClientFactoryMock = new Mock<IHttpClientFactory>();
53-
httpClientFactoryMock
54-
.Setup(f => f.CreateClient(It.IsAny<string>()))
55-
.Returns(httpClient);
56-
services.AddSingleton(httpClientFactoryMock.Object);
57-
services.AddLogging();
58-
59-
// Setup session and HttpContext
60-
var sessionMock = new Mock<ISession>();
61-
sessionMock.Setup(x => x.TryGetValue("AuthToken", out It.Ref<byte[]>.IsAny))
62-
.Returns((string _, out byte[] value) => { value = Encoding.UTF8.GetBytes("test-token"); return true; });
63-
sessionMock.Setup(x => x.TryGetValue("Username", out It.Ref<byte[]>.IsAny))
64-
.Returns((string _, out byte[] value) => { value = Encoding.UTF8.GetBytes("testuser"); return true; });
65-
66-
var httpContextMock = new Mock<HttpContext>();
67-
httpContextMock.Setup(x => x.Session).Returns(sessionMock.Object);
68-
69-
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
70-
httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
71-
services.AddSingleton(httpContextAccessorMock.Object);
72-
73-
// Setup dependent services
74-
var customerServiceMock = new Mock<ICustomerService>();
75-
services.AddSingleton(customerServiceMock.Object);
76-
77-
var productServiceMock = new Mock<IProductService>();
78-
services.AddSingleton(productServiceMock.Object);
79-
80-
var authServiceMock = new Mock<IAuthService>();
81-
82-
if (setupAuthSuccess)
83-
{
84-
// Mock ExecuteWithTokenRefreshAsync to always return success for tests that need it
85-
authServiceMock
86-
.Setup(x => x.ExecuteWithTokenRefreshAsync(
87-
It.IsAny<Func<string, Task<HttpResponseMessage>>>(),
88-
It.IsAny<string>(),
89-
It.IsAny<string>(),
90-
It.IsAny<HttpContext>()))
91-
.ReturnsAsync((true, new HttpResponseMessage
92-
{
93-
StatusCode = HttpStatusCode.OK,
94-
Content = new StringContent("true"),
95-
}));
96-
}
97-
98-
services.AddSingleton(authServiceMock.Object);
9920
services.AddTransient<IOrderService, OrderService>();
100-
101-
return services;
10221
}
10322

10423
[Fact]
@@ -151,8 +70,8 @@ public async Task CreateOrderAsync_ReturnsTrue_WhenApiReturnsSuccess()
15170
if (!result)
15271
{
15372
var requestJson = System.Text.Json.JsonSerializer.Serialize(order);
154-
_testOutputHelper.WriteLine($"Order payload: {requestJson}");
155-
_testOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
73+
TestOutputHelper.WriteLine($"Order payload: {requestJson}");
74+
TestOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
15675
}
15776

15877
// Assert
Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,23 @@
11
using System;
22
using System.Net;
3-
using System.Net.Http;
4-
using System.Text;
5-
using System.Threading;
63
using System.Threading.Tasks;
74
using ECommerce.AdminUI.Services;
8-
using Microsoft.AspNetCore.Http;
95
using Microsoft.Extensions.DependencyInjection;
10-
using Moq;
11-
using Moq.Protected;
126
using Xunit;
137
using Xunit.Abstractions;
148

159
namespace ECommerce.AdminUI.IntegrationTests;
1610

17-
public class ProductServiceIntegrationTests
11+
public class ProductServiceIntegrationTests : ServiceIntegrationTestBase
1812
{
19-
private readonly ITestOutputHelper _testOutputHelper;
20-
2113
public ProductServiceIntegrationTests(ITestOutputHelper testOutputHelper)
14+
: base(testOutputHelper)
2215
{
23-
_testOutputHelper = testOutputHelper;
2416
}
2517

26-
private HttpClient CreateMockHttpClient(HttpStatusCode statusCode, string content)
18+
protected override void ConfigureServices(IServiceCollection services)
2719
{
28-
var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
29-
handlerMock
30-
.Protected()
31-
.Setup<Task<HttpResponseMessage>>(
32-
"SendAsync",
33-
ItExpr.IsAny<HttpRequestMessage>(),
34-
ItExpr.IsAny<CancellationToken>())
35-
.ReturnsAsync(new HttpResponseMessage
36-
{
37-
StatusCode = statusCode,
38-
Content = new StringContent(content),
39-
});
40-
41-
return new HttpClient(handlerMock.Object)
42-
{
43-
BaseAddress = new Uri("https://fake-api/")
44-
};
45-
}
46-
47-
private IServiceCollection SetupCommonServices(HttpClient httpClient, bool setupAuthSuccess = false)
48-
{
49-
var services = new ServiceCollection();
50-
var httpClientFactoryMock = new Mock<IHttpClientFactory>();
51-
httpClientFactoryMock
52-
.Setup(f => f.CreateClient(It.IsAny<string>()))
53-
.Returns(httpClient);
54-
services.AddSingleton(httpClientFactoryMock.Object);
55-
services.AddLogging();
56-
57-
var sessionMock = new Mock<ISession>();
58-
sessionMock.Setup(x => x.TryGetValue("AuthToken", out It.Ref<byte[]>.IsAny))
59-
.Returns((string _, out byte[] value) => { value = Encoding.UTF8.GetBytes("test-token"); return true; });
60-
sessionMock.Setup(x => x.TryGetValue("Username", out It.Ref<byte[]>.IsAny))
61-
.Returns((string _, out byte[] value) => { value = Encoding.UTF8.GetBytes("testuser"); return true; });
62-
63-
var httpContextMock = new Mock<HttpContext>();
64-
httpContextMock.Setup(x => x.Session).Returns(sessionMock.Object);
65-
66-
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
67-
httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
68-
services.AddSingleton(httpContextAccessorMock.Object);
69-
70-
var orderServiceMock = new Mock<IOrderService>();
71-
services.AddSingleton(orderServiceMock.Object);
72-
var customerServiceMock = new Mock<ICustomerService>();
73-
services.AddSingleton(customerServiceMock.Object);
74-
var authServiceMock = new Mock<IAuthService>();
75-
if (setupAuthSuccess)
76-
{
77-
authServiceMock
78-
.Setup(x => x.ExecuteWithTokenRefreshAsync(
79-
It.IsAny<Func<string, Task<HttpResponseMessage>>>(),
80-
It.IsAny<string>(),
81-
It.IsAny<string>(),
82-
It.IsAny<HttpContext>()))
83-
.ReturnsAsync((true, new HttpResponseMessage
84-
{
85-
StatusCode = HttpStatusCode.OK,
86-
Content = new StringContent("true"),
87-
}));
88-
}
89-
services.AddSingleton(authServiceMock.Object);
9020
services.AddTransient<IProductService, ProductService>();
91-
return services;
9221
}
9322

9423
[Fact]
@@ -133,8 +62,8 @@ public async Task CreateProductAsync_ReturnsTrue_WhenApiReturnsSuccess()
13362
if (!result)
13463
{
13564
var requestJson = System.Text.Json.JsonSerializer.Serialize(product);
136-
_testOutputHelper.WriteLine($"Product payload: {requestJson}");
137-
_testOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
65+
TestOutputHelper.WriteLine($"Product payload: {requestJson}");
66+
TestOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
13867
}
13968

14069
// Assert
@@ -165,12 +94,11 @@ public async Task UpdateProductAsync_ReturnsTrue_WhenApiReturnsSuccess()
16594
if (!result)
16695
{
16796
var requestJson = System.Text.Json.JsonSerializer.Serialize(product);
168-
_testOutputHelper.WriteLine($"Product payload: {requestJson}");
169-
_testOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
97+
TestOutputHelper.WriteLine($"Product payload: {requestJson}");
98+
TestOutputHelper.WriteLine("Stubbed HTTP response: 200 OK, body: 'true'");
17099
}
171100

172101
// Assert
173102
Assert.True(result);
174103
}
175104
}
176-

0 commit comments

Comments
 (0)