public void Should_get_injected_mock_When_multiple_dependencies_of_the_same_type()
        {
            //ARRANGE
            var builder = new AutoMock <Target_SameTypeDependency>();

            //ACT
            builder.SelectConstructor();

            //ASSERT
            Assert.IsNotNull(builder.GetMock <IDependency>("d1"));
            Assert.IsNotNull(builder.GetMock <IDependency>("d2"));
        }
        public void Should_throw_When_GetMock_called_before_Building_target2()
        {
            //ARRANGE
            var builder = new AutoMock <Target>();

            //ACT
            Assert.That(() => builder.GetMock <Exception>("parameterName"), Throws.TypeOf <InvalidOperationException>());
        }
        public void Should_throw_When_getting_dependency_registered_under_multiple_names()
        {
            //ARRANGE
            var builder = new AutoMock <Target_SameTypeDependency>();

            //ACT
            builder.SelectConstructor();
            Assert.That(() => builder.GetMock <IDependency>(), Throws.TypeOf <InvalidOperationException>());
        }
        public void Should_throw_when_getting_not_created_mock_parameter_name()
        {
            //ARRANGE
            var builder = new AutoMock <Target>();

            //ACT
            builder.SelectConstructor();
            Assert.That(() => builder.GetMock <IDependency>("notExistingName"), Throws.TypeOf <InvalidOperationException>());
        }
        public void Should_create_mocks_build_target_and_inject_mocks_to_constructor_selected_by_function()
        {
            // ASSERT
            var builder = new AutoMock <Driver>();

            builder.SelectConstructor(SelectConstructorFunc);
            var driver = builder.CreateTarget();

            //ACT
            driver.Drive();

            //ASSERT
            Assert.IsNotNull(builder.GetMock <IVehicle>());
            Assert.IsNotNull(builder.GetMock <IDrivingLicense>());

            builder.GetMock <IVehicle>()
            .Received()
            .Drive();
        }
Ejemplo n.º 6
0
        public void Should_create_mocks_build_target_and_inject_mocks_to_constructor_with_two_dependencies()
        {
            // ASSERT
            var builder = new AutoMock <Driver>();

            builder.SelectConstructor(2);
            var driver = builder.CreateTarget();

            //ACT
            driver.Drive();

            //ASSERT
            Assert.IsNotNull(builder.GetMock <IVehicle>());
            Assert.IsNotNull(builder.GetMock <IDrivingLicense>());

            builder.GetMock <IVehicle>()
            .Received()
            .Drive();
        }
        public void SavingNewEntryWillCreateNewRepoEntity()
        {
            const string slug = "this-is-a-new-slug";

            var mocker = new AutoMock <BlogEntryService>();

            mocker.GetMock <IGetCurrentUserName>().Setup(x => x.GetUserName()).Returns("Jon");
            mocker.GetMock <IUserService <BlogUser, BlogUserEntity, int> >().Setup(x => x.FindByName("Jon")).Returns(new BlogUser {
                DisplayName = "Jon Hawkins"
            });

            mocker.GetMock <IBlogRepository>().Setup(x => x.All <TagEntity>()).Returns(new List <TagEntity> {
                new TagEntity {
                    Id = 1, Name = "c#"
                }, new TagEntity {
                    Id = 3, Name = "php"
                }, new TagEntity {
                    Id = 5, Name = "javascript"
                }
            });
            mocker.GetMock <IBlogRepository>().Setup(x => x.Exists(It.IsAny <Expression <Func <BlogEntryEntity, bool> > >())).Returns(false);

            var blogEntryModel = new BlogEntryModel();

            blogEntryModel.Slug       = slug;
            blogEntryModel.Title      = "this is a new slug";
            blogEntryModel.Html       = "some html";
            blogEntryModel.Date       = DateTime.Now.ToString("dd/MM/yyyy");
            blogEntryModel.TagsString = "c# happy";

            var serviceResult = mocker.Object.Save(blogEntryModel);

            Assert.IsNotNull(serviceResult);

            mocker.GetMock <IBlogRepository>().Verify(x => x.Save(It.Is <BlogEntryEntity>(b =>
                                                                                          b.Slug == blogEntryModel.Slug &&
                                                                                          b.Title == blogEntryModel.Title &&
                                                                                          b.Tags.Count == 2 &&
                                                                                          b.Tags.Any(t => t.Name == "c#" && t.Id == 1) &&
                                                                                          b.Tags.Any(t => t.Name == "happy" && t.Id == 0)
                                                                                          ), It.IsAny <Expression <Func <BlogEntryEntity, bool> > >()));
        }
        public void Should_use_custom_mock_factory()
        {
            //ARRANGE
            var builder = new AutoMock <Target_ConcreteDependency>(new CustomMockFactory());

            //ACT
            builder.SelectConstructor();

            //ASSERT
            Assert.IsNotNull(builder.GetMock <DependencyImplementation1>());
        }
        public void Should_get_injected_mock()
        {
            //ARRANGE
            var builder = new AutoMock <Target>();

            //ACT
            builder.SelectConstructor();

            //ASSERT
            Assert.IsNotNull(builder.GetMock <IDependency>());
        }
        public void Should_create_mocks_build_target_and_inject_dependencies_and_mocks()
        {
            // ASSERT
            var container = new DependencyContainer();

            container.AddDependencyInstance(new FancyGloves());

            var builder = new AutoMock <Driver>(container);

            builder.SelectConstructor();
            var driver = builder.CreateTarget();

            //ACT
            driver.Drive();

            //ASSERT
            Assert.IsNotNull(builder.GetMock <IVehicle>());
            Assert.IsNotNull(builder.GetMock <IDrivingLicense>());

            builder.GetMock <IVehicle>()
            .Received()
            .Drive();
        }
        public void should_use_custom_mock_and_dependency_container()
        {
            //ARRANGE
            var container = new DependencyContainer();

            container.AddDependencyInstance(new DependencyImplementation1());

            var builder = new AutoMock <Target_TwoConcreteDependencies>(new CustomMockFactory(), container);

            //ACT
            builder.SelectConstructor();

            //ASSERT
            Assert.IsNotNull(builder.GetMock <DependencyImplementation2>());
        }
        public void TestSavingWillRecountTags()
        {
            const string slug = "this-is-a-slug";

            var mocker = new AutoMock <BlogEntryService>();

            mocker.GetMock <IGetCurrentUserName>().Setup(x => x.GetUserName()).Returns("Jon");
            mocker.GetMock <IUserService <BlogUser, BlogUserEntity, int> >().Setup(x => x.FindByName("Jon")).Returns(new BlogUser {
                DisplayName = "Jon Hawkins"
            });

            mocker.GetMock <IBlogRepository>().Setup(x => x.All <TagEntity>()).Returns(new List <TagEntity> {
                new TagEntity {
                    Id = 1, Name = "c#"
                }, new TagEntity {
                    Id = 3, Name = "php"
                }, new TagEntity {
                    Id = 5, Name = "javascript"
                }
            });
            mocker.GetMock <IBlogRepository>().Setup(x => x.Exists(It.IsAny <Expression <Func <BlogEntryEntity, bool> > >())).Returns(true);
            mocker.GetMock <IBlogRepository>().Setup(x => x.Single(It.IsAny <Expression <Func <BlogEntryEntity, bool> > >())).Returns(new BlogEntryEntity {
                Slug = slug
            });


            var blogEntryModel = new BlogEntryModel();

            blogEntryModel.Slug       = slug;
            blogEntryModel.Html       = "some html";
            blogEntryModel.Date       = DateTime.Now.ToString("dd/MM/yyyy");
            blogEntryModel.TagsString = "c# happy";

            var serviceResult = mocker.Object.Save(blogEntryModel);

            Assert.IsNotNull(serviceResult);
            Assert.IsTrue(serviceResult.Succeeded);

            mocker.GetMock <IBlogTagService>().Verify(x => x.UpdateTagCount());
            mocker.GetMock <IBlogTagService>().Verify(x => x.RemoveUnsedTags());
        }
        public void TestAddNewTagWillSaveTagEntity()
        {
            const string slug = "this-is-a-slug";

            var mocker = new AutoMock <BlogEntryService>();

            mocker.GetMock <IGetCurrentUserName>().Setup(x => x.GetUserName()).Returns("Jon");
            mocker.GetMock <IUserService <BlogUser, BlogUserEntity, int> >().Setup(x => x.FindByName("Jon")).Returns(new BlogUser {
                DisplayName = "Jon Hawkins"
            });

            mocker.GetMock <IBlogRepository>().Setup(x => x.All <TagEntity>()).Returns(new List <TagEntity> {
                new TagEntity {
                    Id = 1, Name = "c#"
                }, new TagEntity {
                    Id = 3, Name = "php"
                }, new TagEntity {
                    Id = 5, Name = "javascript"
                }
            });
            mocker.GetMock <IBlogRepository>().Setup(x => x.Exists(It.IsAny <Expression <Func <BlogEntryEntity, bool> > >())).Returns(true);
            mocker.GetMock <IBlogRepository>().Setup(x => x.Single(It.IsAny <Expression <Func <BlogEntryEntity, bool> > >())).Returns(new BlogEntryEntity {
                Slug = slug
            });


            var blogEntryModel = new BlogEntryModel();

            blogEntryModel.Slug       = slug;
            blogEntryModel.Html       = "some html";
            blogEntryModel.Date       = DateTime.Now.ToString("dd/MM/yyyy");
            blogEntryModel.TagsString = "c# happy";

            mocker.Object.Save(blogEntryModel);

            mocker.GetMock <IBlogRepository>().Verify(x => x.Add(It.Is <TagEntity>(t => t.Name == "happy")));
        }