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); }); }
public void Leave_withUnknownRole_throwsArgumentException() { // arrange: FakeWorkingSession session = GeneratePartialMock <FakeWorkingSession>(); IFakeRole achievedRole = GenerateStrictMock <IFakeRole>(); // assert: Assert.Throws <ArgumentException>(delegate { session.Leave <IFakeRole>(ref achievedRole); }); }
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); }
public void Serialize_afterAnAchieve_works() { // arrange: IWorkingSession session = new FakeSerializableWorkingSession(); IFakeRole achievedRole = null; session.Achieve <IFakeRole>(out achievedRole); // act: Stream stream = TestUtilities.Serialize(session); // assert: Assert.IsNotNull(stream); }
public void Build_withoutPrincipal_callTemplateMethod() { // arrange: IPrincipal player = null; FakeRole role = MockRepository.GeneratePartialMock <FakeRole>(); FakeRoleBuilder <IFakeRole, FakeRole> builder = MockRepository.GeneratePartialMock <FakeRoleBuilder <IFakeRole, FakeRole> >(); builder.Expect(b => b.CallCreateRoleFor(player)).Return(role).Repeat.Once(); // act: IFakeRole returnedRole = builder.Build(player); // assert: Assert.AreSame(role, returnedRole); }
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); }); }
public void Deserialize_afterAnAchieve_works() { // arrange: IWorkingSession session = new FakeSerializableWorkingSession(); IFakeRole achievedRole = null; session.Achieve <IFakeRole>(out achievedRole); Stream stream = TestUtilities.Serialize(session); // act: IFakeRole deserializedRole = null; IWorkingSession deserialized = TestUtilities.Deserialize <IWorkingSession>(stream); deserialized.Achieve <IFakeRole>(out deserializedRole); // assert: Assert.IsNotNull(deserialized); Assert.AreEqual(((FakeSerializableWorkingSession)session).CalledMethods.Count(), ((FakeSerializableWorkingSession)deserialized).CalledMethods.Count()); }
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); }
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); }
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); } }
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); }
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); }