Sorry to confuse you...I may adding a bit of explanation as some info is missing.
In BeforeTest method I mock IPrincipal object. I call IsInRole method in my BO in Autorization method (eg. CanAddObject()). The fake result is substitute at line #7.
In Save method of my BO I read ApplicationContext.User.Identity.Name value. I tried to put this piece of code ( Expect.Call(mockPrincipal.Identity.Name).Return("ju"); ) after line #7, but it gave me an error. (Null reference, I guess because the IIdentity is not created)
I just suppose I need to mock IIdentity object and add the reference to ApplicationContext in same way.
#1 public void BeforeTest()
#2 {
#3 mocks = new MockRepository();
#4 IPrincipal mockPrincipal = mocks.CreateMock<IPrincipal>();
#5 ApplicationContext.User = mockPrincipal;
#6 using (mocks.Record()) {
#7 Expect.Call(mockPrincipal.IsInRole(Roles.ROLE_MAN_PERSON)).Return(true);
#8 }
#9 }
That's a bit more for decription. Thanks for any helps.
Ah ha! My problem is that I did not take a close enough look at the code. At least one of your problems is in deed that you did not mock the IIdentity object. The specific line in question is:
_updatedBy = ApplicationContext.User.Identity.Name;
This is going through the IPrincipal object in ApplicationContext.User to get the IIdentity object and then trying to get the name from the the IIdentity object. The IIdentity object is null in this context and you end up with a null reference exception.
You need to mock the IIdentity object and specifically the Name property on that interface. You can use something like RhinoMocks for that but I would suggest that you write a dummy TestPrincipal and TestIdentity. The test versions of these classes are very simple and most CSLA classes typically have a fair amount of authorization-based behavior that is controlled by the behavior of the IPrincipal and IIdentity objects. I find it easier to create stubs that I can control than to repeat the mocking logic each time. But you do what makes sense for you.
Jon Stonecash
Copyright (c) Marimer LLC