Exemple #1
0
        public void Test()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddTransient(typeof(IMainModel), typeof(MainModel));

            var faker = new Autofaker();

            faker.UseNSubstitute();
            faker.UseMicrosoftDependencyInjection(serviceCollection);

            faker.RegisterFakesForConstructorParameterTypesOf <IMainModel>();

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var fakeFirstDependency = serviceProvider.GetService <IFirstDependencyModel>();

            fakeFirstDependency.SayFoo().Returns("fakefoo");

            var fakeSecondDependency = serviceProvider.GetService <ISecondDependencyModel>();

            fakeSecondDependency.SayBar().Returns("fakebar");

            var mainModel = serviceProvider.GetService <IMainModel>();

            Assert.AreEqual("fakefoofakebar", mainModel.SayStuff());
        }
Exemple #2
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();
        }
        public void Test()
        {
            var builder = new ContainerBuilder();

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

            var faker = new Autofaker();

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

            faker.RegisterFakesForConstructorParameterTypesOf <IMainModel>();

            var container = builder.Build();

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

            Assert.AreEqual(1, fakeEnumerable.Length);

            var fakeEnumerableInstance = fakeEnumerable.Single();

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

            fakeFirstDependency.SayFoo().Returns("fakefoo");

            Assert.AreSame(fakeFirstDependency, fakeEnumerableInstance);

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

            fakeSecondDependency.SayBar().Returns("fakebar");

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

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