Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ICalculator, Calculator>()
.Configure<Options>(config => configuration.GetSection("Options").Bind(config));

protected override string GetConfigurationFile()
=> "appsettings.json";
}
}
Original file line number Diff line number Diff line change
@@ -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<TestFixture>
{
public IntegrationTests(ITestOutputHelper testOutputHelper, TestFixture fixture)
: base(testOutputHelper, fixture)
{
}

[Theory]
[InlineData(1, 2)]
public void Test1(int x, int y)
{
var calculator = _fixture.GetService<ICalculator>(_testOutputHelper);
var option = _fixture.GetService<IOptions<Options>>(_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<ICalculator>(_testOutputHelper);
var option = _fixture.GetScopedService<IOptions<Options>>(_testOutputHelper);
var calculated = calculator.Add(x, y);
var expected = option.Value.Rate * (x + y);
Assert.True(expected == calculated);
}

protected override void Clear()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Options> option)
=> _option = option.Value;

public int Add(int x, int y)
=> (x + y) * _option.Rate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Services
{
public interface ICalculator
{
int Add(int x, int y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Services
{
public class Options
{
public int Rate { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="Xunit.Microsoft.DependencyInjection" Version="1.0.0.32" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.10" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.10" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.10" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.10" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.10" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.10" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.10" />
</ItemGroup>

<ItemGroup>
<Folder Include="Fixtures\" />
<Folder Include="Services\" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Options": {
"Rate" : 10
}
}
6 changes: 6 additions & 0 deletions src/Xunit.Microsoft.DependencyInjection.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down