Skip to content

Commit 6fca970

Browse files
committed
Add ITestContextFacade
1 parent be37ce4 commit 6fca970

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

src/Dibix.Testing/Http/WebApiTestBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ namespace Dibix.Testing.Http
1717
private protected async Task ExecuteTest<TTestContext>(Func<TTestContext, Task> testFlow, Func<IHttpClientFactory, HttpClientOptions, IHttpAuthorizationProvider, TTestContext> contextCreator) where TTestContext : HttpTestContext
1818
{
1919
IHttpClientFactory httpClientFactory = TestHttpClientFactoryBuilder.Create(TestContext, Out)
20-
.Configure(x => ConfigureClient(Configuration, x))
20+
.Configure(ConfigureClient)
2121
.Build();
2222
HttpClientOptions httpClientOptions = new HttpClientOptions();
2323
ConfigureOptions(httpClientOptions);
2424
ITestAuthorizationContext authorizationContext = new TestAuthorizationContext(httpClientFactory, httpClientOptions);
25-
IHttpAuthorizationProvider authorizationProvider = await Authorize(authorizationContext, Configuration).ConfigureAwait(false);
25+
IHttpAuthorizationProvider authorizationProvider = await Authorize(authorizationContext).ConfigureAwait(false);
2626
TTestContext testContext = contextCreator(httpClientFactory, httpClientOptions, authorizationProvider);
2727
await testFlow(testContext).ConfigureAwait(false);
2828
}
@@ -62,9 +62,9 @@ protected async Task<TResponseContent> InvokeApiAndAssertResponse<TService, TRes
6262
return responseContent;
6363
}
6464

65-
protected virtual void ConfigureClient(TConfiguration configuration, IHttpClientBuilder builder) { }
65+
protected virtual void ConfigureClient(IHttpClientBuilder builder) { }
6666

67-
protected virtual Task<IHttpAuthorizationProvider> Authorize(ITestAuthorizationContext context, TConfiguration configuration) => Task.FromResult<IHttpAuthorizationProvider>(null);
67+
protected virtual Task<IHttpAuthorizationProvider> Authorize(ITestAuthorizationContext context) => Task.FromResult<IHttpAuthorizationProvider>(null);
6868
#endregion
6969

7070
#region Private Methods
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace Dibix.Testing
5+
{
6+
public interface ITestContextFacade
7+
{
8+
TestContext TestContext { get; }
9+
TextWriter Out { get; }
10+
TestClassInstanceScope Scope { get; }
11+
12+
string AddTestFile(string fileName, string content);
13+
string AddTestRunFile(string fileName);
14+
string ImportTestRunFile(string filePath);
15+
}
16+
17+
public interface ITestContextFacade<out TConfiguration> : ITestContextFacade
18+
{
19+
TConfiguration Configuration { get; }
20+
}
21+
}

src/Dibix.Testing/TestBase.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace Dibix.Testing
1919
{
20-
public abstract class TestBase : IDisposable
20+
public abstract class TestBase : ITestContextFacade, IDisposable
2121
{
2222
#region Fields
2323
private readonly Assembly _assembly;
@@ -265,6 +265,17 @@ private static T SafeGetProperty<T>(ref T field, [CallerMemberName] string prope
265265
}
266266
#endregion
267267

268+
#region ITestContextFacade Members
269+
TestContext ITestContextFacade.TestContext => TestContext;
270+
TextWriter ITestContextFacade.Out => Out;
271+
TestClassInstanceScope ITestContextFacade.Scope => Scope;
272+
273+
string ITestContextFacade.AddTestFile(string fileName, string content) => AddTestFile(fileName, content);
274+
string ITestContextFacade.AddTestRunFile(string fileName) => AddTestRunFile(fileName);
275+
276+
string ITestContextFacade.ImportTestRunFile(string filePath) => ImportTestRunFile(filePath);
277+
#endregion
278+
268279
#region IDisposable Members
269280
public void Dispose()
270281
{
@@ -287,7 +298,7 @@ protected virtual void Dispose(bool disposing)
287298
}
288299
#endregion
289300
}
290-
public abstract class TestBase<TConfiguration> : TestBase where TConfiguration : class, new()
301+
public abstract class TestBase<TConfiguration> : TestBase, ITestContextFacade<TConfiguration> where TConfiguration : class, new()
291302
{
292303
private TConfiguration _configuration;
293304

@@ -318,5 +329,7 @@ private void AddConfigurationToOutput(TConfiguration configuration)
318329
if (Scope == TestClassInstanceScope.TestInitialize)
319330
_ = AddTestFile("appsettings.json", JsonConvert.SerializeObject(configuration, Formatting.Indented));
320331
}
332+
333+
TConfiguration ITestContextFacade<TConfiguration>.Configuration => Configuration;
321334
}
322335
}

0 commit comments

Comments
 (0)