Skip to content

Commit bc50721

Browse files
Merge pull request #105 from IntelliTect/add-overload
Additional AddService and AddInstance methods
2 parents 7163a1a + d010251 commit bc50721

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

IntelliTect.TestTools.TestFramework/IntelliTect.TestTools.TestFramework.Tests/ExampleDataThing.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
namespace IntelliTect.TestTools.TestFramework.Tests
22
{
3-
public class ExampleDataThing
3+
public interface IExampleDataInterface
4+
{
5+
string Testing { get; set; }
6+
}
7+
8+
public class ExampleDataThing : IExampleDataInterface
49
{
510
public string Testing { get; set; } = "Testing";
611
}

IntelliTect.TestTools.TestFramework/IntelliTect.TestTools.TestFramework.Tests/TestBuilderTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ public void FetchByServiceForExecuteArg()
105105
.ExecuteTestCase();
106106
}
107107

108+
[Fact]
109+
public void FetchByImplementationForExecuteArg()
110+
{
111+
TestBuilder builder = new TestBuilder();
112+
builder
113+
.AddDependencyInstance<IExampleDataInterface>(new ExampleDataThing())
114+
.AddTestBlock<ExampleTestBlockWithExecuteArgForInterface>()
115+
.ExecuteTestCase();
116+
}
117+
118+
[Fact]
119+
public void FetchByImplementationAndTypeForExecuteArg()
120+
{
121+
TestBuilder builder = new TestBuilder();
122+
builder
123+
.AddDependencyService<IExampleDataInterface, ExampleDataThing>()
124+
.AddTestBlock<ExampleTestBlockWithExecuteArgForInterface>()
125+
.ExecuteTestCase();
126+
}
127+
108128
// This test probably isn't necessary. This is MS DI out-of-the-box functionality
109129
[Fact]
110130
public void FetchByFactoryForConstructor()
@@ -375,6 +395,15 @@ public void Execute(ExampleDataThing input)
375395
}
376396
}
377397

398+
public class ExampleTestBlockWithExecuteArgForInterface : ITestBlock
399+
{
400+
public void Execute(IExampleDataInterface input)
401+
{
402+
if (input == null) throw new ArgumentNullException(nameof(input));
403+
Assert.Equal("Testing", input.Testing);
404+
}
405+
}
406+
378407
public class ExampleTestBlockWithPropertyForOwnType : ITestBlock
379408
{
380409
public ExampleDataThing Input { get; set; }

IntelliTect.TestTools.TestFramework/IntelliTect.TestTools.TestFramework/TestBuilder.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public TestBuilder AddDependencyService<T>()
7979
return this;
8080
}
8181

82+
public TestBuilder AddDependencyService<TServiceType,TImplementationType>()
83+
{
84+
Services.AddScoped(typeof(TServiceType), typeof(TImplementationType));
85+
return this;
86+
}
87+
8288
/// <summary>
8389
/// Adds an instance of a Type to the container that is needed for a TestBlock to execute
8490
/// </summary>
@@ -92,6 +98,12 @@ public TestBuilder AddDependencyInstance(object objToAdd)
9298
return this;
9399
}
94100

101+
public TestBuilder AddDependencyInstance<T>(object objToAdd)
102+
{
103+
Services.AddSingleton(typeof(T), objToAdd);
104+
return this;
105+
}
106+
95107
// Are there other cases where we'll need to add something at this level?
96108
// If so, this shouldn't be called "AddLogger".
97109
// Might need to make this scoped. It's behaving oddly when running tests in parallel

0 commit comments

Comments
 (0)