AggregateServiceFixture.cs :  » Inversion-of-Control-Dependency-Injection » Autofac » AutofacContrib » Tests » AggregateService » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Autofac 
Autofac » AutofacContrib » Tests » AggregateService » AggregateServiceFixture.cs
using System;
using Autofac;
using AutofacContrib.AggregateService;
using Moq;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace AutofacContrib.Tests.AggregateService{
    [TestFixture]
    public class AggregateServiceFixture
    {
        private IContainer _container;
        private ISomeDependency _someDependencyMock;
        private IMyContext _aggregateService;

        [SetUp]
        public void Setup()
        {
            _someDependencyMock = new Mock<ISomeDependency>().Object;

            var builder = new ContainerBuilder();
            builder.RegisterAggregateService<IMyContext>();
            builder.RegisterType<MyServiceImpl>()
                .As<IMyService>()
                .InstancePerDependency();
            builder.RegisterInstance(_someDependencyMock);
            _container = builder.Build();

            _aggregateService = _container.Resolve<IMyContext>();
        }

        [Test]
        public void Property_ResolvesService()
        {
            Assert.That(_aggregateService.MyService, 
                Is.Not.Null & Is.InstanceOfType(typeof(IMyService)));
        }

        [Test]
        public void Property_Getter_AlwaysReturnSameInstance()
        {
            var firstInstance = _aggregateService.MyService;
            var secondInstance = _aggregateService.MyService;

            Assert.That(firstInstance, Is.SameAs(secondInstance));
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void Property_Setter_Throws()
        {
            _aggregateService.PropertyWithSetter = null;
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void Method_WithVoid_Throws()
        {
            _aggregateService.MethodWithoutReturnValue();
        }

        [Test]
        public void Method_ResolvesService()
        {
            Assert.That(_aggregateService.GetMyService(), 
                Is.Not.Null & Is.InstanceOfType(typeof(IMyService)));
        }

        [Test]
        public void Method_WithParameter_PassesParameterToService()
        {
            var myService = _aggregateService.GetMyService(10);

            Assert.That(myService.SomeIntValue, Is.EqualTo(10));
        }

        [Test]
        public void Method_WithParameters_PassesParametersToService()
        {
            var someDate = DateTime.Now;
            var myService = _aggregateService.GetMyService(someDate, 20);

            Assert.That(myService.SomeDateValue, Is.EqualTo(someDate));
            Assert.That(myService.SomeIntValue, Is.EqualTo(20));
        }

        [Test]
        public void Method_WithNullParameters_PassesParametersToService()
        {
            var myService = _aggregateService.GetMyService(null);

            Assert.That(myService.SomeStringValue, Is.Null);
        }

        [Test]
        public void Method_WithParameter_PassesParameterAndOtherDependenciesToService()
        {
            var myService = _aggregateService.GetMyService("text");

            Assert.That(myService.SomeStringValue, Is.EqualTo("text"));
            Assert.That(myService.SomeDependency, Is.EqualTo(_someDependencyMock));
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.