Exemplo n.º 1
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();
        }
Exemplo n.º 2
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 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")));
        }
Exemplo n.º 4
0
        public void ShouldAssertOccurrenceFromContainerWithoutPriorArrangement()
        {
            var c = new MockingContainer <Unit>();

            c.Instance.DoWork();
            c.Assert <IUnitOfWork>(x => x.DoWork());
        }
Exemplo n.º 5
0
        public void ShouldAssertMultipleInstancesByName()
        {
            var container = new MockingContainer <TransactionService>();

            container.Bind <IAccount>().ToMock().InjectedIntoParameter("fromAccount").Named("from")
            .AndArrange(x => Mock.Arrange(() => x.Id).Returns(10));
            container.Bind <IAccount>().ToMock().InjectedIntoParameter("toAccount").Named("to")
            .AndArrange(x => Mock.Arrange(() => x.Id).Returns(20));
            container.Bind <IAccount>().ToMock().InjectedIntoProperty((TransactionService s) => s.BillingAccount).Named("bill")
            .AndArrange(x => Mock.Arrange(() => x.Id).Returns(30));

            var inst = container.Instance;

            inst.TransferFunds(10);

            container.Assert <IAccount>("from", x => x.Withdraw(11), Occurs.Once());
            container.Assert <IAccount>("to", x => x.Deposit(10), Occurs.Once());
            container.Assert <IAccount>("bill", x => x.Deposit(1), Occurs.Once());
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
0
		public void ShouldAssertThatGetReportsIsCalled()
		{
			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>())).Occurs(2);

			container.Instance.SendReports();

			container.Assert<IReportBuilder>(rb => rb.GetReports());
		}
Exemplo n.º 8
0
		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>()));
		}
Exemplo n.º 9
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!"));
        }
Exemplo n.º 10
0
        public void ShouldArrangePropertySetWithMatcher()
        {
            // Arrange
            var container = new MockingContainer <Module>();

            container.ArrangeSet <IService>(x => x.Value = Arg.AnyInt).MustBeCalled();
            var service = container.Get <IService>();

            // Act
            service.Value = 99;

            // Assert
            container.Assert <IService>();
        }
Exemplo n.º 11
0
        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());
        }
Exemplo n.º 12
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());
        }
Exemplo n.º 13
0
        public void ShouldTransferFundsBetweenTwoAccounts()
        {
            // ARRANGE
            // Creating a MockingContainer of AccountService.
            // To instantiate the system under test (the container) you should use the Instance property
            // For example: container.Instance.
            var container = new MockingContainer <AccountService>();

            decimal expectedBalance = 100;

            container.Bind <Account>().ToMock().InjectedIntoParameter("fromAccount")
            .AndArrange(x =>
            {
                // Arranging: That the Balance property from the Account instance
                //              must be called from the container during the test method
                //              and it should return expectedBalance.
                Mock.Arrange(() => x.Balance).Returns(expectedBalance).MustBeCalled();

                // Arranging: That the Withdraw() method from the Account instance
                //              must be called from the container during the test method.
                Mock.Arrange(() => x.Withdraw(expectedBalance)).MustBeCalled();
            });

            container.Bind <Account>().ToMock().InjectedIntoParameter("toAccount")
            .AndArrange(x =>
            {
                // Arranging: That the Deposit() method from the Account instance
                //              must be called from the container during the test method.
                Mock.Arrange(() => x.Deposit(expectedBalance)).MustBeCalled();
            });

            // ACT - Calling TransferFunds() from the mocked instance of AccountService
            container.Instance.TransferFunds(expectedBalance);

            // ASSERT - Asserting all expectations for the container.
            container.Assert();
        }
		public void ShouldAssertOccurrenceFromContainerWithoutPriorArrangement()
		{
			var c = new MockingContainer<Unit>();
			c.Instance.DoWork();
			c.Assert<IUnitOfWork>(x => x.DoWork());
		}
Exemplo n.º 15
0
		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 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 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());
		}
		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();
		}
		public void ShouldAssertMultipleInstancesByName()
		{
			var container = new MockingContainer<TransactionService>();
			container.Bind<IAccount>().ToMock().InjectedIntoParameter("fromAccount").Named("from")
				.AndArrange(x => Mock.Arrange(() => x.Id).Returns(10));
			container.Bind<IAccount>().ToMock().InjectedIntoParameter("toAccount").Named("to")
				.AndArrange(x => Mock.Arrange(() => x.Id).Returns(20));
			container.Bind<IAccount>().ToMock().InjectedIntoProperty((TransactionService s) => s.BillingAccount).Named("bill")
				.AndArrange(x => Mock.Arrange(() => x.Id).Returns(30));

			var inst = container.Instance;

			inst.TransferFunds(10);

			container.Assert<IAccount>("from", x => x.Withdraw(11), Occurs.Once());
			container.Assert<IAccount>("to", x => x.Deposit(10), Occurs.Once());
			container.Assert<IAccount>("bill", x => x.Deposit(1), Occurs.Once());
		}
Exemplo n.º 21
0
		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 ShouldResolveTargetTypeWithInterfaceAndConcreteDependencies()
		{
			var container = new MockingContainer<Unit>();

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

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

			container.Assert();
		}