Exemple #1
0
        public void Leave_withUnknownRole_throwsArgumentException()
        {
            // arrange:
            FakeWorkingSession session      = GeneratePartialMock <FakeWorkingSession>();
            IFakeRole          achievedRole = GenerateStrictMock <IFakeRole>();

            // assert:
            Assert.Throws <ArgumentException>(delegate {
                session.Leave <IFakeRole>(ref achievedRole);
            });
        }
Exemple #2
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 #3
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 #4
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);
        }