Skip to content

Commit

Permalink
Added tests for nested app using IoC
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Fazlani committed Jan 9, 2018
1 parent 1f65512 commit 5da6d42
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CommandDotNet.Tests/CommandDotNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="CommandDotNet.IoC.Autofac" Version="0.0.2-alpha" />
<PackageReference Include="CommandDotNet.IoC.MicrosoftDependencyInjection" Version="0.0.4-alpha" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CommandDotNet\CommandDotNet.csproj" />
Expand Down
55 changes: 55 additions & 0 deletions CommandDotNet.Tests/IoCTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Autofac;
using CommandDotNet.Attributes;
using CommandDotNet.IoC.Autofac;
using FluentAssertions;
using Xunit;

namespace CommandDotNet.Tests
{
public class IoCTests
{
[Fact]
public void CanResolveDependencyInNestedCommand()
{
ContainerBuilder containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<Service>().As<IService>();
IContainer container = containerBuilder.Build();

AppRunner<NestedCommandApp> appRunner = new AppRunner<NestedCommandApp>().UseAutofac(container);

appRunner.Run("InnerApp", "-a", "3", "Process").Should().Be(7);
}
}

public class NestedCommandApp
{
public class InnerApp
{
private readonly int _additionFactor;
public IService Service { get; set; }

public InnerApp([Option(ShortName = "a")]int additionFactor)
{
_additionFactor = additionFactor;
}

public int Process()
{
return Service.GetValue() + _additionFactor;
}
}
}

public interface IService
{
int GetValue();
}

public class Service : IService
{
public int GetValue()
{
return 4;
}
}
}

0 comments on commit 5da6d42

Please sign in to comment.