PageRenderTime 15ms CodeModel.GetById 12ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/UnitTests/IOC/FieldInjectionTests.cs

http://github.com/philiplaureano/LinFu
C# | 32 lines | 24 code | 5 blank | 3 comment | 0 complexity | 6f93ddafe5d3fff6b2ad321fda6615d2 MD5 | raw file
 1using System;
 2using LinFu.IoC;
 3using Xunit;
 4using SampleLibrary;
 5using SampleLibrary.IOC;
 6
 7namespace LinFu.UnitTests.IOC
 8{
 9    public class FieldInjectionTests : BaseTestFixture
10    {
11        [Fact]
12        public void ShouldAutoInjectField()
13        {
14            var container = new ServiceContainer();
15            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
16
17            var instance = new SampleClassWithInjectionField();
18
19            // Initialize the container
20            container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest();
21            container.Inject<ISampleService>("MyService").Using(c => instance).OncePerRequest();
22
23            var result = container.GetService<ISampleService>("MyService");
24            Assert.Same(result, instance);
25
26            // On initialization, the instance.SomeField value
27            // should be a SampleClass type
28            Assert.NotNull(instance.SomeField);
29            Assert.Equal(typeof(SampleClass), instance?.SomeField?.GetType());
30        }
31    }
32}