PageRenderTime 68ms CodeModel.GetById 63ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/UnitTests/IOC/FluentPropertyInjectionTests.cs

http://github.com/philiplaureano/LinFu
C# | 51 lines | 39 code | 8 blank | 4 comment | 1 complexity | 79cb22db5a775073cfdc474102b30ebd MD5 | raw file
 1using LinFu.IoC;
 2using Moq;
 3using Xunit;
 4using SampleLibrary;
 5
 6namespace LinFu.UnitTests.IOC
 7{
 8    public class FluentPropertyInjectionTests
 9    {
10        private static void TestPropertyInjection(string serviceName)
11        {
12            var mockTarget = new Mock<IInjectionTarget>();
13            mockTarget.Expect(t => t.SetValue(123));
14            var target = mockTarget.Object;
15
16            var container = new ServiceContainer();
17            container.AddService(serviceName, target);
18
19            // Use the named fluent interface for
20            // named instances
21            if (!string.IsNullOrEmpty(serviceName))
22                container.Initialize<IInjectionTarget>(serviceName)
23                    .With(service => service.SetValue(123));
24            else
25                container.Initialize<IInjectionTarget>()
26                    .With(service => service.SetValue(123));
27            var result = container.GetService<IInjectionTarget>(serviceName);
28            Assert.NotNull(result);
29
30            // The container should initialize the
31            // service on every request
32            mockTarget.VerifyAll();
33        }
34
35        [Fact]
36        public void NamedPropertyMustBeInjectedIntoInjectionTarget()
37        {
38            var serviceName = "MyService";
39
40            TestPropertyInjection(serviceName);
41        }
42
43        [Fact]
44        public void UnnamedPropertyMustBeInjectedIntoInjectionTarget()
45        {
46            string serviceName = null;
47
48            TestPropertyInjection(serviceName);
49        }
50    }
51}