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 property and one method of ApplicationUser object. ApplicationUser is inherited from IPrincipal. The properties are: 1) ApplicationContext.User.IsInRole(...) - the method is part of IPrincipal 2) ApplicationContext.User.Identity.Name - the name is property of IIdentity which is part of ApplicationUser aka IPricipal
Example of my test (using RhinoMock):
public void BeforeTest()
{
mocks = new MockRepository();
IPrincipal mockPrincipal = mocks.CreateMock<IPrincipal>();
ApplicationContext.User = mockPrincipal;
using (mocks.Record()) {
Expect.Call(mockPrincipal.IsInRole(Roles.ROLE_MAN_PERSON)).Return(true);
Expect.Call(mockPrincipal.Identity.Name).Return("ju"); //doesn't work!!!! return null ref exc
}
}
I have slight problem with second value, the identity name. I tried to mock it but have problem to assign mocked IIdentity to ApplicationUser, as it is done internaly. I was told to just create some IIPrincipal (including IIdentity) by myself and not to mock it at all. Which can be done for sure. Not sure if this can be called as Stub using?
So can you advice me how to deal with IPrincipal and IIdentity? Any suggestion most welcome.