Beispiel #1
0
        public void ShouldCreateMocksOfDependencies()
        {
            var container = new MockingContainer <FileLog>();

            container.Arrange <IFileSystem>(x => x.Exists("123")).Returns(true);
            container.Arrange <ICalendar>(x => x.Now).Returns(new DateTime(123));

            Assert.True(container.Instance.LogExists());
        }
		public void ShouldCreateMocksOfDependencies()
		{
			var container = new MockingContainer<FileLog>();

			container.Arrange<IFileSystem>(x => x.Exists("123")).Returns(true);
			container.Arrange<ICalendar>(x => x.Now).Returns(new DateTime(123));

			Assert.True(container.Instance.LogExists());
		}
		public void ShouldAssertThatReportsAreReceived()
		{
			var container = new MockingContainer<Reporter>();

			container.Arrange<IReportBuilder>(rb => rb.GetReports()).Returns(new[] { new Report(), new Report() }).MustBeCalled();
			container.Arrange<IReportSender>(rs => rs.SendReport(Arg.IsAny<Report>()));

			container.Instance.SendReports();

			container.Assert<IReportBuilder>();
		}
		public void ShouldAssertSpecificallyThatSendReportMethodIsCalledTwice()
		{
			var container = new MockingContainer<Reporter>();

			container.Arrange<IReportBuilder>(rb => rb.GetReports()).Returns(new[] { new Report(), new Report() });
			container.Arrange<IReportSender>(rs => rs.SendReport(Arg.IsAny<Report>())).Occurs(2);

			container.Instance.SendReports();

			container.Assert<IReportSender>(rs => rs.SendReport(Arg.IsAny<Report>()));
		}
		public void ShouldAssertThatReportIsSent()
		{
			var container = new MockingContainer<Reporter>();

			container.Arrange<IReportBuilder>(rb => rb.GetReports()).Returns(new[] { new Report(), new Report() });
			container.Arrange<IReportSender>(rs => rs.SendReport(Arg.IsAny<Report>())).Occurs(2);

			container.Instance.SendReports();

			container.AssertAll();
		}
Beispiel #6
0
        public void ShouldIncludeAssertionMessageWhenAssertingContainer()
        {
            var c = new MockingContainer <FileLog>();

            c.Arrange <ICalendar>(x => x.Now).MustBeCalled("Calendar must be used!");
            c.Arrange <IFileSystem>(x => x.Refresh()).MustBeCalled("Should use latest data!");

            var ex = Assert.Throws <AssertionException>(() => c.Assert("Container must be alright!"));

            Assert.True(ex.Message.Contains("Calendar must be used!"));
            Assert.True(ex.Message.Contains("Should use latest data!"));
            Assert.True(ex.Message.Contains("Container must be alright!"));
        }
		public void ShouldAssertArrangedExpectations()
		{
			var container = new MockingContainer<FileLog>();

			container.Arrange<IFileSystem>(x => x.Refresh()).MustBeCalled();
			container.Arrange<IFileSystem>(x => x.Exists("123")).Returns(true).MustBeCalled();
			container.Arrange<ICalendar>(x => x.Now).Returns(new DateTime(123)).MustBeCalled();

			Assert.Throws<AssertFailedException>(() => container.Assert());

			container.Instance.LogExists();

			container.Assert();
		}
Beispiel #8
0
        public void ShouldAssertArrangedExpectations()
        {
            var container = new MockingContainer <FileLog>();

            container.Arrange <IFileSystem>(x => x.Refresh()).MustBeCalled();
            container.Arrange <IFileSystem>(x => x.Exists("123")).Returns(true).MustBeCalled();
            container.Arrange <ICalendar>(x => x.Now).Returns(new DateTime(123)).MustBeCalled();

            Assert.Throws <AssertionException>(() => container.Assert());

            container.Instance.LogExists();

            container.Assert();
        }
        public void GetTodoItems()
        {
            // arrange
            TodoList todoList          = dataPrep.TodoLists.Create();
            int      expectedItemCount = 5;
            IEnumerable <TodoItem> expectedItemList = dataPrep.TodoItems.CreateManyForList(expectedItemCount, todoList);

            mockContainer.Arrange <ITodoItemAccessor>(accessor => accessor.GetTodoItemsForList(todoList.Id)).Returns(expectedItemList);

            // act
            IEnumerable <TodoItem> actualItemList = manager.GetTodoItems(todoList.Id);

            //assert
            expectedItemList.ShouldDeepEqual(actualItemList);
        }
        public void Test()
        {
            // Arrange
            using var container = new MockingContainer <MyClassAuto>();

            container.Arrange <INameProvider>(x => x.GetName()).Returns("A Name").MustBeCalled();
            // or
            //var nameProviderMock = Mock.Create<INameProvider>();
            //nameProviderMock.Arrange(x => x.GetName()).Returns("A Name").MustBeCalled();
            //container.Bind<INameProvider>().ToConstant(nameProviderMock);

            // Act
            var sut = container.Instance;

            sut.DoGreeting();

            // Assert
            container.Assert();

            var greetingServiceMock = container.Get <IGreetingService>();

            greetingServiceMock.Assert(x => x.Greet("A Name"));

            //Assert.Equal(1, Mock.GetTimesCalled(() => greetingServiceMock.Greet("A Name")));

            // This throws an exception
            //Assert.Equal(1, Mock.GetTimesCalled(() => container.Get<IGreetingService>().Greet("A Name")));
        }
Beispiel #11
0
        public void Setup()
        {
            tempDir = TempDirectoriesFactory.CreateEmpty();

            container = new MockingContainer <ChangelogManager>();
            container.Arrange <IBinDirectory>(directory => directory.FullPath).Returns(tempDir.FullName);

            WriteSampleChangelog();
        }
Beispiel #12
0
        public void ShouldResolveTargetTypeWithInterfaceAndConcreteDependencies()
        {
            var container = new MockingContainer <Unit>();

            container.Arrange <IUnitOfWork>(uow => uow.DoWork()).MustBeCalled();

            // this is where it resolves.
            container.Instance.DoWork();

            container.Assert();
        }
Beispiel #13
0
        public void ShouldArrangeSingletonInstances()
        {
            var container = new MockingContainer <TransactionService>();

            container.Arrange <IAccount>(x => x.Id).Returns(10);

            var inst = container.Instance;

            Assert.NotNull(inst.From);
            Assert.Same(inst.From, inst.To);
            Assert.Same(inst.From, inst.BillingAccount);
        }
Beispiel #14
0
        public void ShouldAssertRaisesAgainstMethod()
        {
            var container = new MockingContainer <Executor>();

            bool raised = false;

            container.Arrange <IExecutor>(x => x.Submit()).Raises(() => container.Get <IExecutor>().Done += null, EventArgs.Empty);

            container.Get <IExecutor>().Done += delegate { raised = true; };

            container.Instance.Submit();

            Assert.True(raised);
        }
Beispiel #15
0
        public void ShouldAssertMockingNestedDependency()
        {
            var container = new MockingContainer <Foo>();

            container.Bind <Bar>().ToSelf();

            container.Arrange <IUnitOfWork>(uow => uow.DoWork()).MustBeCalled();

            Assert.Throws <AssertionException>(() => container.Assert());

            container.Instance.DoWork();

            container.Assert();
        }
        public void WithAutomocking()
        {
            //Arrange
            var container = new MockingContainer<ClassUnderTest>();
            var expectedResult = "Hello";

            container
                .Arrange<ISecondDependency>(x => x.GetString())
                .Returns(expectedResult);

            //Act
            var result = container.Instance.StringMethod();

            //Assert
            Assert.AreEqual(expectedResult, result);
        }
Beispiel #17
0
        public void WithAutomocking()
        {
            //Arrange
            var container      = new MockingContainer <ClassUnderTest>();
            var expectedResult = "Hello";

            container
            .Arrange <ISecondDependency>(x => x.GetString())
            .Returns(expectedResult);

            //Act
            var result = container.Instance.StringMethod();

            //Assert
            Assert.AreEqual(expectedResult, result);
        }
Beispiel #18
0
        public void ShouldAssertDependenciesDirectly()
        {
            var container = new MockingContainer <FileLog>();

            container.Arrange <ICalendar>(x => x.Now).Returns(new DateTime(123));

            Assert.Throws <AssertionException>(() => container.Assert <IFileSystem>(x => x.Refresh(), Occurs.Once()));
            Assert.Throws <AssertionException>(() => container.Assert <IFileSystem>(x => x.Exists("123"), Occurs.Once()));
            Assert.Throws <AssertionException>(() => container.Assert <ICalendar>(x => x.Now, Occurs.Once()));

            container.Instance.LogExists();

            container.Assert <IFileSystem>(x => x.Refresh(), Occurs.Once());
            container.Assert <IFileSystem>(x => x.Exists("123"), Occurs.Once());
            container.Assert <ICalendar>(x => x.Now, Occurs.Once());
        }
        public void ShouldAutoMockConcretedDependecies()
        {
            // ARRANGE
            // Creating a MockingContainer of PersonRepository.
            // To instantiate the system under test (the container) you should use the Instance property
            // For example: container.Instance.
            var container = new MockingContainer <PersonRepository>();

            // Arranging: That the Walk() method from the Person instance
            //              must be called from the container during the test method.
            container.Arrange <Person>(anml => anml.Walk()).MustBeCalled();

            // ACT - Calling Walk() from the mocked instance of PersonRepository
            container.Instance.Walk();

            // ASSERT - Asserting all expectations against this method.
            container.Assert <Person>(anml => anml.Walk());
        }
        public void ShouldAssertAllContainerArrangments()
        {
            // ARRANGE
            // Creating a MockingContainer of ClassUnderTest.
            // To instantiate the system uder test (the container) you should use the Instance property
            // For example: container.Instance.
            var container = new MockingContainer <ClassUnderTest>();

            // Arranging: That the GetString() method from the ISecondDependecy interface
            //              must be called from the container instance durring the test method.
            container.Arrange <ISecondDependency>(
                secondDep => secondDep.GetString()).MustBeCalled();

            // ACT - Calling SringMethod() from the mocked instance of ClassUnderTest
            var actualString = container.Instance.StringMethod();

            // ASSERT - Asserting all expectations for the container.
            container.AssertAll();
        }
Beispiel #21
0
        public void ShouldAssertAllContainerArrangments()
        {
            // ARRANGE
            // Creating a MockingContainer of ClassUnderTest. 
            // To instantiate the system uder test (the container) you should use the Instance property 
            // For example: container.Instance. 
            var container = new MockingContainer<ClassUnderTest>();
             
            // Arranging: That the GetString() method from the ISecondDependecy interface 
            //              must be called from the container instance durring the test method. 
            container.Arrange<ISecondDependency>(
               secondDep => secondDep.GetString()).MustBeCalled();

            // ACT - Calling SringMethod() from the mocked instance of ClassUnderTest
            var actualString = container.Instance.StringMethod();

            // ASSERT - Asserting all expectations for the container.
            container.AssertAll();
        }
Beispiel #22
0
        public void ShouldMockDependenciesWithContainer()
        {
            // ARRANGE
            // Creating a MockingContainer of ClassUnderTest. 
            // To instantiate the system uder test (the container) you should use the Instance property 
            // For example: container.Instance. 
            var container = new MockingContainer<ClassUnderTest>();

            string expectedString = "Test";

            // Arranging: When the GetString() method from the ISecondDependecy interface 
            //              is called from the container, it should return expectedString. 
            container.Arrange<ISecondDependency>(
               secondDep => secondDep.GetString()).Returns(expectedString);

            // ACT - Calling SringMethod() from the mocked instance of ClassUnderTest
            var actualString = container.Instance.StringMethod();

            // ASSERT
            Assert.AreEqual(expectedString, actualString);
        }
        public void ShouldMockDependenciesWithContainer()
        {
            // ARRANGE
            // Creating a MockingContainer of ClassUnderTest.
            // To instantiate the system uder test (the container) you should use the Instance property
            // For example: container.Instance.
            var container = new MockingContainer <ClassUnderTest>();

            string expectedString = "Test";

            // Arranging: When the GetString() method from the ISecondDependecy interface
            //              is called from the container, it should return expectedString.
            container.Arrange <ISecondDependency>(
                secondDep => secondDep.GetString()).Returns(expectedString);

            // ACT - Calling SringMethod() from the mocked instance of ClassUnderTest
            var actualString = container.Instance.StringMethod();

            // ASSERT
            Assert.AreEqual(expectedString, actualString);
        }
		public void ShouldAssertMockingNestedDependency()
		{
			var container = new MockingContainer<Foo>();

			//container.Arrange<Bar>(uow => uow.DoWork()).CallOriginal().MustBeCalled();
			container.Arrange<IUnitOfWork>(uow => uow.DoWork()).MustBeCalled();

			container.Instance.DoWork();

			container.Assert();
		}
		public void ShouldArrangeRaisesBehaviorOnSecondInstance()
		{
			var amount = 100M;
			var container = new MockingContainer<BankTransation>();

			container.Arrange<IAccount>(x => x.Withdraw(amount),
				Take.First())
					 .Returns(true)
					 .Occurs(1);

			container.Arrange<IAccount>(x => x.Deposit(amount),
				Take.At(1))
					 .Raises(() => container.Get<IAccount>(Take.At(1)).DepositMade += null, amount)
					 .Returns(true)
					 .Occurs(1);

			bool eventRaised = false;
			container.Get<IAccount>(Take.At(1)).DepositMade += d => eventRaised = true;

			container.Instance.TransferFunds(amount);

			Assert.True(eventRaised);
		}
		public void ShouldAssertFundTransfersBetweenTwoAccounts()
		{
			var container = new MockingContainer<AccountService>();

			decimal expectedBalance = 100;

			container.Arrange<IAcccount>(x => x.Balance, Take.First()).Returns(expectedBalance);
			container.Arrange<IAcccount>(x => x.Withdraw(expectedBalance), Take.First()).MustBeCalled();
			container.Arrange<IAcccount>(x => x.Deposit(expectedBalance), Take.Last()).MustBeCalled();

			container.Instance.TransferFunds(expectedBalance);

			container.Assert();
		}
		public void ShouldIncludeAssertionMessageWhenAssertingContainer()
		{
			var c = new MockingContainer<FileLog>();
			c.Arrange<ICalendar>(x => x.Now).MustBeCalled("Calendar must be used!");
			c.Arrange<IFileSystem>(x => x.Refresh()).MustBeCalled("Should use latest data!");

			var ex = Assert.Throws<AssertFailedException>(() => c.Assert("Container must be alright!"));

			Assert.True(ex.Message.Contains("Calendar must be used!"));
			Assert.True(ex.Message.Contains("Should use latest data!"));
			Assert.True(ex.Message.Contains("Container must be alright!"));
		}
		public void ShouldAssertRaisesAgainstMethod()
		{
			var container = new MockingContainer<Executor>();

			bool raised = false;

			container.Arrange<IExecutor>(x => x.Submit()).Raises(() => container.Get<IExecutor>().Done += null, EventArgs.Empty);

			container.Get<IExecutor>().Done += delegate { raised = true; };

			container.Instance.Submit();

			Assert.True(raised);
		}
		public void ShouldAssertMockingNestedDependency()
		{
			var container = new MockingContainer<Foo>();
			container.Bind<Bar>().ToSelf();

			container.Arrange<IUnitOfWork>(uow => uow.DoWork()).MustBeCalled();

			Assert.Throws<AssertFailedException>(() => container.Assert());

			container.Instance.DoWork();

			container.Assert();
		}
		public void ShouldResolveTargetTypeWithInterfaceAndConcreteDependencies()
		{
			var container = new MockingContainer<Unit>();

			container.Arrange<IUnitOfWork>(uow => uow.DoWork()).MustBeCalled();

			// this is where it resolves.
			container.Instance.DoWork();

			container.Assert();
		}
		public void ShouldArrangeSingletonInstances()
		{
			var container = new MockingContainer<TransactionService>();
			container.Arrange<IAccount>(x => x.Id).Returns(10);

			var inst = container.Instance;
			Assert.NotNull(inst.From);
			Assert.Same(inst.From, inst.To);
			Assert.Same(inst.From, inst.BillingAccount);
		}
		public void ShouldAssertDependenciesDirectly()
		{
			var container = new MockingContainer<FileLog>();

			container.Arrange<ICalendar>(x => x.Now).Returns(new DateTime(123));

			Assert.Throws<AssertFailedException>(() => container.Assert<IFileSystem>(x => x.Refresh(), Occurs.Once()));
			Assert.Throws<AssertFailedException>(() => container.Assert<IFileSystem>(x => x.Exists("123"), Occurs.Once()));
			Assert.Throws<AssertFailedException>(() => container.Assert<ICalendar>(x => x.Now, Occurs.Once()));

			container.Instance.LogExists();

			container.Assert<IFileSystem>(x => x.Refresh(), Occurs.Once());
			container.Assert<IFileSystem>(x => x.Exists("123"), Occurs.Once());
			container.Assert<ICalendar>(x => x.Now, Occurs.Once());
		}