コード例 #1
0
        public void Initialize()
        {
            _service = new GlobalFunctionParser();

            var builder = new ContainerBuilder();

            var faker = new Autofaker();

            faker.UseAutofac(builder);
            faker.UseNSubstitute();

            faker.RegisterFakesForConstructorParameterTypesOf <GlobalFunctionParserTests>();

            _container = builder.Build();
        }
コード例 #2
0
        public void Test()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType <MainModel>().As <IMainModel>();

            var faker = new Autofaker();

            faker.UseMoq();
            faker.UseAutofac(builder);

            faker.RegisterFakesForConstructorParameterTypesOf <IMainModel>();

            var container = builder.Build();

            var fakeFirstDependencyMock  = container.Resolve <Mock <IFirstDependencyModel> >();
            var fakeSecondDependencyMock = container.Resolve <Mock <ISecondDependencyModel> >();

            var fakeFirstDependencyMockInterface  = container.Resolve <Mock <IFirstDependencyModel> >();
            var fakeSecondDependencyMockInterface = container.Resolve <Mock <ISecondDependencyModel> >();

            var fakeEnumerable = container
                                 .Resolve <IEnumerable <IFirstDependencyModel> >()
                                 .ToArray();

            Assert.AreEqual(1, fakeEnumerable.Length);

            var fakeEnumerableInstance = fakeEnumerable.Single();

            Assert.AreSame(fakeFirstDependencyMock, fakeFirstDependencyMockInterface);
            Assert.AreSame(fakeSecondDependencyMock, fakeSecondDependencyMockInterface);

            fakeFirstDependencyMock.Setup(x => x.SayFoo()).Returns("fakefoo");
            fakeSecondDependencyMock.Setup(x => x.SayBar()).Returns("fakebar");

            var fakeFirstDependencyInstance = container.Resolve <IFirstDependencyModel>();

            Assert.AreEqual("fakefoo", fakeFirstDependencyInstance.SayFoo());
            Assert.AreSame(fakeFirstDependencyInstance, fakeEnumerableInstance);

            var fakeSecondDependencyInstance = container.Resolve <ISecondDependencyModel>();

            Assert.AreEqual("fakebar", fakeSecondDependencyInstance.SayBar());

            var mainModel = container.Resolve <IMainModel>();

            Assert.AreEqual("fakefoofakebar", mainModel.SayStuff());
        }