public void ShouldArrangeMultipleInstancesSeparatelyByParameterName() { var container = new MockingContainer <TransactionService>(); container.Bind <IAccount>().ToMock().InjectedIntoParameter("fromAccount").AndArrange(x => Mock.Arrange(() => x.Id).Returns(10)); container.Bind <IAccount>().ToMock().InjectedIntoParameter("toAccount").AndArrange(x => Mock.Arrange(() => x.Id).Returns(20)); container.Bind <IAccount>().ToMock().InjectedIntoProperty((TransactionService s) => s.BillingAccount).AndArrange(x => Mock.Arrange(() => x.Id).Returns(30)); var inst = container.Instance; Assert.Equal(10, inst.From.Id); Assert.Equal(20, inst.To.Id); Assert.Equal(30, inst.BillingAccount.Id); }
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()); }
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 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 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()); }
public void ShouldArrangeMultipleInstancesSeparatelyByPropertyName() { var container = new MockingContainer<TransactionService>(); container.Bind<IAccount>().ToMock().InjectedIntoProperty("BillingAccount").AndArrange(x => Mock.Arrange(() => x.Id).Returns(30)); var inst = container.Instance; Assert.Equal(30, inst.BillingAccount.Id); }
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(); }