Mock IIdentity and IPrincipal

前端 未结 2 625
故里飘歌
故里飘歌 2021-02-08 12:46

I just wanna ask what would be better approach to supply these objects in my unit tests.

In my unit test I am testing CSLA object. CSLA object is internally using one pr

2条回答
  •  死守一世寂寞
    2021-02-08 13:33

    Here is the code I use to return a test user (using Stubs):

        [SetUp]
        public void Setup()
        {
            var identity = MockRepository.GenerateStub();
            identity.Stub(p => p.Name).Return("TestUser").Repeat.Any();
            var principal = MockRepository.GenerateStub();
            principal.Stub(p => p.Identity).Return(identity).Repeat.Any();
    
            Thread.CurrentPrincipal = principal;
        }
    

    I've got linq in other code so I'm using the var type for the variables; just substitute the correct types (IPrincipal, IIdentity) if needed.

提交回复
热议问题