tand/test/Tand.Extensions.DependencyInjection.Tests/TandServiceExtensionsTests.cs

61 lines
2.1 KiB
C#
Raw Normal View History

2020-03-10 00:01:59 +00:00
using System;
using Microsoft.Extensions.DependencyInjection;
using Tand.Core;
using Tand.Core.Api;
2020-03-10 00:01:59 +00:00
using Tand.Core.Models;
using Xunit;
namespace Tand.Extensions.DependencyInjection.Tests
{
public class TandServiceExtensionsTests
{
public const string Greeting = "Hello, World!";
[Fact]
public void RegisterService_RegisterSampleService_SuccessfullyResolve()
{
var calledOnEnter = false;
var calledOnExit = false;
var servides = new ServiceCollection();
servides.AddTand();
servides.AddSingleton(new TestDecorator(_ => { calledOnEnter = true;}, _ => { calledOnExit = true;}));
servides.AddTandTransient<ISampleService, SampleServiceImpl>();
var provider = servides.BuildServiceProvider();
var sampleService = provider.GetService<ISampleService>();
Assert.NotNull(sampleService);
var result = sampleService.Greet();
Assert.Equal(Greeting, result);
Assert.True(calledOnEnter);
Assert.True(calledOnExit);
}
}
public class TestDecorator : ITandTarget<ISampleService>
{
private readonly Action<CallEnterContext<ISampleService>> _onEnterHandle;
private readonly Action<CallLeaveContext<ISampleService>> _onLeaveHandle;
public TestDecorator(Action<CallEnterContext<ISampleService>> onEnterHandle, Action<CallLeaveContext<ISampleService>> onLeaveHandle)
{
_onEnterHandle = onEnterHandle;
_onLeaveHandle = onLeaveHandle;
}
public void OnEnterMethod(CallEnterContext<ISampleService> enterContext) => _onEnterHandle(enterContext);
public void OnLeaveMethod(CallLeaveContext<ISampleService> leaveContext) => _onLeaveHandle(leaveContext);
}
public interface ISampleService
{
[Tand<TestDecorator>]
2020-03-10 00:01:59 +00:00
string Greet();
}
public class SampleServiceImpl : ISampleService
{
public string Greet() => TandServiceExtensionsTests.Greeting;
}
}