Ejemplo n.º 1
0
        public void SetUp()
        {
            this.actionExecutingContext = new ActionExecutingContext
            {
                ActionParameters = new Dictionary <string, object>()
            };
            this.userStoreMock = MockRepository.GenerateMock <IUserStore <ApplicationUser> >();
            this.dataProtectionProviderMock = MockRepository.GenerateMock <IDataProtectionProvider>();
            var dataProtector = MockRepository.GenerateMock <IDataProtector>();

            this.dataProtectionProviderMock.Expect(mock => mock.Create(Arg <string> .Is.Anything)).Return(dataProtector);
            this.userManager       = new ApplicationUserManager(this.userStoreMock, this.dataProtectionProviderMock);
            clientIdCalculatorMock = MockRepository.GenerateMock <ClientIdCalculator>();
            //need to simulate like the parameter exists on the method
            this.actionExecutingContext.ActionParameters[UserContextAttribute.USER_CONTEXT_KEY] = null;

            HttpContextBase httpContextBase = MockRepository.GenerateMock <HttpContextBase>();

            this.actionExecutingContext.HttpContext = httpContextBase;

            IPrincipal principal = MockRepository.GenerateMock <IPrincipal>();

            httpContextBase.Expect(contextBase => contextBase.User)
            .Repeat.Any()
            .Return(principal);
            this.identity = MockRepository.GenerateMock <IIdentity>();
            principal.Expect(mock => mock.Identity)
            .Repeat.Any()
            .Return(this.identity);
            this.identity.Expect(mock => mock.IsAuthenticated)
            .Repeat.Once()
            .Return(true);

            HttpRequestBase requestBaseMock = MockRepository.GenerateMock <HttpRequestBase>();

            httpContextBase.Expect(mock => mock.Request)
            .Return(requestBaseMock);
            this.requestParameters = new NameValueCollection();
            requestBaseMock.Expect(mock => mock.Params)
            .Return(this.requestParameters);

            this.userContextActionFilter = new UserContextAttribute();
            this.applicationUser         = new ApplicationUser()
            {
                Id = "user id",
                CurrentGamingGroupId = 315
            };
            Task <ApplicationUser> task = Task.FromResult(this.applicationUser);

            //TODO can't figure out how to mock the GetUserId() extension method, so have to be less strict here
            this.userStoreMock.Expect(mock => mock.FindByIdAsync(Arg <string> .Is.Anything))
            .Repeat.Once()
            .Return(task);
        }
Ejemplo n.º 2
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();
            }
        }
 protected void AddRole(string role)
 {
     _principalStub.Expect(principal => principal.IsInRole(role)).Return(true).Repeat.Any();
 }