Exemple #1
0
        public void Dispose_causeAchievedRolesDisposition()
        {
            // arrange:
            FakeRole roleToReturn = GeneratePartialMock <FakeRole>();

            roleToReturn.Expect(r => r.Dispose()).Repeat.Once();
            FakeWorkingSession fakeSession = GeneratePartialMock <FakeWorkingSession>();
            IPrincipal         owner       = fakeSession.Owner;

            fakeSession.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(true).Repeat.Once();
            FakeRoleBuilder <IFakeRole, FakeRole> roleBuilder = GeneratePartialMock <FakeRoleBuilder <IFakeRole, FakeRole> >();

            roleBuilder.Expect(b => b.CallCreateRoleFor(owner)).Return(roleToReturn).Repeat.Once();
            fakeSession.Expect(s => s.CallGetRoleBuilder <IFakeRole>()).Return(roleBuilder).Repeat.Once();
            IFakeRole achievedRole = null;

            fakeSession.Achieve <IFakeRole>(out achievedRole);
            fakeSession.Expect(s => s.CallBeforeDispose()).Repeat.Once();
            IWorkingSession session = fakeSession;
            IFakeRole       roleRef = null;

            // act:
            fakeSession.Dispose();

            // assert:
            Assert.Throws <ObjectDisposedException>(delegate { session.CanAchieve <IFakeRole>(); });
            Assert.Throws <ObjectDisposedException>(delegate { session.Achieve <IFakeRole>(out roleRef); });
            Assert.IsNull(roleRef);
            Assert.Throws <ObjectDisposedException>(delegate { session.Leave <IFakeRole>(ref roleRef); });
        }
Exemple #2
0
        public void Achieve_newAllowedRole_delegateToRoleBuilder()
        {
            // arrange:
            FakeRole           roleToReturn = new FakeRole();
            FakeWorkingSession session      = GeneratePartialMock <FakeWorkingSession>();
            IPrincipal         owner        = session.Owner;

            session.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(true).Repeat.Once();
            FakeRoleBuilder <IFakeRole, FakeRole> roleBuilder = GeneratePartialMock <FakeRoleBuilder <IFakeRole, FakeRole> >();

            roleBuilder.Expect(b => b.CallCreateRoleFor(owner)).Return(roleToReturn).Repeat.Once();
            session.Expect(s => s.CallGetRoleBuilder <IFakeRole>()).Return(roleBuilder).Repeat.Once();
            IFakeRole achievedRole = null;

            // act:
            session.Achieve <IFakeRole>(out achievedRole);

            // assert:
            Assert.AreSame(roleToReturn, achievedRole);
        }
Exemple #3
0
        public void CanAchieve_anAlreadyAchievedRole_dontCallIsAllowedTemplateMethod()
        {
            // arrange:
            FakeRole           roleToReturn = new FakeRole();
            FakeWorkingSession session      = GeneratePartialMock <FakeWorkingSession>();
            IPrincipal         owner        = session.Owner;

            session.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(true).Repeat.Once();
            FakeRoleBuilder <IFakeRole, FakeRole> roleBuilder = GeneratePartialMock <FakeRoleBuilder <IFakeRole, FakeRole> >();

            roleBuilder.Expect(b => b.CallCreateRoleFor(owner)).Return(roleToReturn).Repeat.Once();
            session.Expect(s => s.CallGetRoleBuilder <IFakeRole>()).Return(roleBuilder).Repeat.Once();
            IFakeRole achievedRole1 = null;

            session.Achieve <IFakeRole>(out achievedRole1);

            // act:
            bool canAchieve = session.CanAchieve <IFakeRole>();

            // assert:
            Assert.IsTrue(canAchieve);
        }
Exemple #4
0
        public void CanAchieve_newRole_callIsAllowedTemplateMethod(bool isAllowed)
        {
            // arrange:
            FakeWorkingSession session = GeneratePartialMock <FakeWorkingSession>();

            session.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(isAllowed).Repeat.Once();

            // act:
            bool canAchieve = session.CanAchieve <IFakeRole>();

            // assert:
            Assert.AreEqual(isAllowed, canAchieve);
        }
Exemple #5
0
        public void Leave_withRoleAchievedManyTimes_disposeRoleOnlyOnce(int times)
        {
            // arrange:
            FakeRole roleToReturn = GeneratePartialMock <FakeRole>();

            roleToReturn.Expect(r => r.Dispose()).Repeat.Once();                                                        // Dispose expected only once.
            FakeWorkingSession session = GeneratePartialMock <FakeWorkingSession>();
            IPrincipal         owner   = session.Owner;

            session.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(true).Repeat.Once();
            FakeRoleBuilder <IFakeRole, FakeRole> roleBuilder = GeneratePartialMock <FakeRoleBuilder <IFakeRole, FakeRole> >();

            roleBuilder.Expect(b => b.CallCreateRoleFor(owner)).Return(roleToReturn).Repeat.Once();
            session.Expect(s => s.CallGetRoleBuilder <IFakeRole>()).Return(roleBuilder).Repeat.Once();
            IFakeRole achievedRole = null;

            for (int i = 0; i < times; ++i)
            {
                IFakeRole achievedRoleI = null;
                session.Achieve <IFakeRole>(out achievedRoleI);
                if (i == 0)
                {
                    achievedRole = achievedRoleI;
                }
                else
                {
                    Assert.AreSame(achievedRole, achievedRoleI);
                }
            }

            // assert:
            for (int i = 0; i < times; ++i)
            {
                IFakeRole achievedRoleI2 = achievedRole;
                session.Leave <IFakeRole>(ref achievedRoleI2);
                Assert.IsNull(achievedRoleI2);
            }
        }
Exemple #6
0
        public void Leave_withUnknownRoleReference_throwsArgumentException()
        {
            // arrange:
            IFakeRole          unknownInstance = new FakeRole();
            FakeRole           roleToReturn    = GeneratePartialMock <FakeRole>();
            FakeWorkingSession session         = GeneratePartialMock <FakeWorkingSession>();
            IPrincipal         owner           = session.Owner;

            session.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(true).Repeat.Once();
            FakeRoleBuilder <IFakeRole, FakeRole> roleBuilder = GeneratePartialMock <FakeRoleBuilder <IFakeRole, FakeRole> >();

            roleBuilder.Expect(b => b.CallCreateRoleFor(owner)).Return(roleToReturn).Repeat.Once();
            session.Expect(s => s.CallGetRoleBuilder <IFakeRole>()).Return(roleBuilder).Repeat.Once();
            IFakeRole achievedRole1 = null;

            session.Achieve <IFakeRole>(out achievedRole1);

            // assert:
            Assert.Throws <ArgumentException>(delegate {
                session.Leave <IFakeRole>(ref unknownInstance);
            });
            Assert.IsNotNull(unknownInstance);
        }
Exemple #7
0
        public void Achieve_notAllowedRole_throwsInvalidOperationException()
        {
            // arrange:
            FakeWorkingSession session = GeneratePartialMock <FakeWorkingSession>();

            session.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(false).Repeat.Once();
            IFakeRole achievedRole = null;

            // assert:
            Assert.Throws <InvalidOperationException>(delegate {
                session.Achieve <IFakeRole>(out achievedRole);
            });
            Assert.IsNull(achievedRole);
        }
Exemple #8
0
        public void Leave_withAchievedRole_disposeRole()
        {
            // arrange:
            FakeRole roleToReturn = GeneratePartialMock <FakeRole>();

            roleToReturn.Expect(r => r.Dispose()).Repeat.Once();
            FakeWorkingSession session = GeneratePartialMock <FakeWorkingSession>();
            IPrincipal         owner   = session.Owner;

            session.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(true).Repeat.Once();
            FakeRoleBuilder <IFakeRole, FakeRole> roleBuilder = GeneratePartialMock <FakeRoleBuilder <IFakeRole, FakeRole> >();

            roleBuilder.Expect(b => b.CallCreateRoleFor(owner)).Return(roleToReturn).Repeat.Once();
            session.Expect(s => s.CallGetRoleBuilder <IFakeRole>()).Return(roleBuilder).Repeat.Once();
            IFakeRole achievedRole1 = null;

            session.Achieve <IFakeRole>(out achievedRole1);

            // act:
            session.Leave <IFakeRole>(ref achievedRole1);

            // assert:
            Assert.IsNull(achievedRole1);
        }
Exemple #9
0
        public void Dispose_callsBeforeDisposeTemplateMethod()
        {
            // arrange:
            FakeWorkingSession fakeSession = GeneratePartialMock <FakeWorkingSession>();

            fakeSession.Expect(s => s.CallBeforeDispose()).Repeat.Once();
            IWorkingSession session = fakeSession;
            IFakeRole       roleRef = null;

            // act:
            fakeSession.Dispose();

            // assert:
            Assert.Throws <ObjectDisposedException>(delegate { session.CanAchieve <IFakeRole>(); });
            Assert.Throws <ObjectDisposedException>(delegate { session.Achieve <IFakeRole>(out roleRef); });
            Assert.IsNull(roleRef);
            Assert.Throws <ObjectDisposedException>(delegate { session.Leave <IFakeRole>(ref roleRef); });
        }
Exemple #10
0
        public void EndWorkingSession_onWorkingSessionException_throwsInvalidOperationException()
        {
            // arrange:
            System.Collections.Generic.List <object> mocks = new System.Collections.Generic.List <object>();
            Exception dummyException = new Exception("dummyException");

            Fakes.FakeEnterprise enterprise = MockRepository.GeneratePartialMock <Fakes.FakeEnterprise>();
            mocks.Add(enterprise);
            IPrincipal owner = MockRepository.GenerateStrictMock <IPrincipal>();

            owner.Expect(p => p.Identity).Return(new GenericIdentity("testPrincipal"));
            mocks.Add(owner);
            FakeWorkingSession session = MockRepository.GeneratePartialMock <FakeWorkingSession>("MockWorkingSession", owner);

            session.Expect(s => s.CallBeforeDispose()).Throw(dummyException).Repeat.Once();
            mocks.Add(session);
            enterprise.Expect(e => e.CallBeforeWorkingSessionEnd(owner, session)).Repeat.Once();
            InvalidOperationException cought = null;

            // act:
            try
            {
                enterprise.EndWorkingSession(owner, session);
            }
            catch (InvalidOperationException e)
            {
                cought = e;
            }

            // assert:
            Assert.IsNotNull(cought);
            Assert.AreSame(dummyException, cought.InnerException);
            foreach (object m in mocks)
            {
                m.VerifyAllExpectations();
            }
        }
Exemple #11
0
        public void EndWorkingSession_withValidArguments_callTemplateMethod()
        {
            // arrange:
            System.Collections.Generic.List <object> mocks = new System.Collections.Generic.List <object>();
            Fakes.FakeEnterprise enterprise = MockRepository.GeneratePartialMock <Fakes.FakeEnterprise>();
            mocks.Add(enterprise);
            IPrincipal owner = MockRepository.GenerateStrictMock <IPrincipal>();

            mocks.Add(owner);
            FakeWorkingSession session = MockRepository.GeneratePartialMock <FakeWorkingSession>("MockWorkingSession", owner);

            session.Expect(s => s.CallBeforeDispose()).Repeat.Once();
            mocks.Add(session);
            enterprise.Expect(e => e.CallBeforeWorkingSessionEnd(owner, session)).Repeat.Once();

            // act:
            enterprise.EndWorkingSession(owner, session);

            // assert:
            foreach (object m in mocks)
            {
                m.VerifyAllExpectations();
            }
        }