diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs new file mode 100644 index 0000000..054d0e4 --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Microsoft.DependencyInjection.Abstracts; +using Xunit.Microsoft.DependencyInjection.ExampleTests.Services; + +namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures +{ + public class TestFixture : TestBedFixture + { + protected override void AddServices(IServiceCollection services, IConfiguration configuration) + => services + .AddTransient() + .Configure(config => configuration.GetSection("Options").Bind(config)); + + protected override string GetConfigurationFile() + => "appsettings.json"; + } +} diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/IntegrationTests.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/IntegrationTests.cs new file mode 100644 index 0000000..b6fe23c --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/IntegrationTests.cs @@ -0,0 +1,43 @@ +using Microsoft.Extensions.Options; +using Xunit.Abstractions; +using Xunit.Microsoft.DependencyInjection.Abstracts; +using Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures; +using Xunit.Microsoft.DependencyInjection.ExampleTests.Services; +using Options = Xunit.Microsoft.DependencyInjection.ExampleTests.Services.Options; + +namespace Xunit.Microsoft.DependencyInjection.ExampleTests +{ + public class IntegrationTests : TestBed + { + public IntegrationTests(ITestOutputHelper testOutputHelper, TestFixture fixture) + : base(testOutputHelper, fixture) + { + } + + [Theory] + [InlineData(1, 2)] + public void Test1(int x, int y) + { + var calculator = _fixture.GetService(_testOutputHelper); + var option = _fixture.GetService>(_testOutputHelper); + var calculated = calculator.Add(x, y); + var expected = option.Value.Rate * (x + y); + Assert.True(expected == calculated); + } + + [Theory] + [InlineData(1, 2)] + public void Test2(int x, int y) + { + var calculator = _fixture.GetScopedService(_testOutputHelper); + var option = _fixture.GetScopedService>(_testOutputHelper); + var calculated = calculator.Add(x, y); + var expected = option.Value.Rate * (x + y); + Assert.True(expected == calculated); + } + + protected override void Clear() + { + } + } +} diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/Calculator.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/Calculator.cs new file mode 100644 index 0000000..a476071 --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/Calculator.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.Options; + +namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Services +{ + public class Calculator : ICalculator + { + private readonly Options _option; + + public Calculator(IOptions option) + => _option = option.Value; + + public int Add(int x, int y) + => (x + y) * _option.Rate; + } +} diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/ICalculator.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/ICalculator.cs new file mode 100644 index 0000000..f109d4a --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/ICalculator.cs @@ -0,0 +1,7 @@ +namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Services +{ + public interface ICalculator + { + int Add(int x, int y); + } +} diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/Options.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/Options.cs new file mode 100644 index 0000000..52e6d2c --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/Options.cs @@ -0,0 +1,7 @@ +namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Services +{ + public class Options + { + public int Rate { get; set; } + } +} diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs new file mode 100644 index 0000000..f4f6a37 --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs @@ -0,0 +1,17 @@ +using Xunit.Microsoft.DependencyInjection.Attributes; + +namespace Xunit.Microsoft.DependencyInjection.ExampleTests +{ + [TestCaseOrderer("Xunit.Microsoft.DependencyInjection.TestsOrder.TestPriorityOrderer", "Xunit.Microsoft.DependencyInjection.TestsOrder")] + [Collection("Dependency Injection")] + public class UnitTests + { + [Fact, TestOrder(1)] + public void Test1() + => Assert.True(1 == 1); + + [Fact, TestOrder(2)] + public void Test2() + => Assert.False(1 == 0); + } +} diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj new file mode 100644 index 0000000..c8bfbd6 --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj @@ -0,0 +1,36 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/appsettings.json b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/appsettings.json new file mode 100644 index 0000000..749b762 --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/appsettings.json @@ -0,0 +1,5 @@ +{ + "Options": { + "Rate" : 10 + } +} diff --git a/src/Xunit.Microsoft.DependencyInjection.sln b/src/Xunit.Microsoft.DependencyInjection.sln index ddd0e41..2041f88 100644 --- a/src/Xunit.Microsoft.DependencyInjection.sln +++ b/src/Xunit.Microsoft.DependencyInjection.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.808.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xunit.Microsoft.DependencyInjection", "Xunit.Microsoft.DependencyInjection.csproj", "{A2F3411E-6B6E-4B7B-BCC1-4F4BA1B345E7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xunit.Microsoft.DependencyInjection.ExampleTests", "..\examples\Xunit.Microsoft.DependencyInjection.ExampleTests\Xunit.Microsoft.DependencyInjection.ExampleTests.csproj", "{B8F50313-1C91-4212-A11A-6D3589BA8DF7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {A2F3411E-6B6E-4B7B-BCC1-4F4BA1B345E7}.Debug|Any CPU.Build.0 = Debug|Any CPU {A2F3411E-6B6E-4B7B-BCC1-4F4BA1B345E7}.Release|Any CPU.ActiveCfg = Release|Any CPU {A2F3411E-6B6E-4B7B-BCC1-4F4BA1B345E7}.Release|Any CPU.Build.0 = Release|Any CPU + {B8F50313-1C91-4212-A11A-6D3589BA8DF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B8F50313-1C91-4212-A11A-6D3589BA8DF7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B8F50313-1C91-4212-A11A-6D3589BA8DF7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B8F50313-1C91-4212-A11A-6D3589BA8DF7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE