模拟身份和IPrincipal [英] Mock IIdentity and IPrincipal
问题描述
我只想问问在单元测试中提供这些对象的更好方法.
I just wanna ask what would be better approach to supply these objects in my unit tests.
在单元测试中,我正在测试CSLA对象. CSLA对象在内部使用ApplicationUser对象的一种属性和一种方法. ApplicationUser继承自IPrincipal. 这些属性是: 1)ApplicationContext.User.IsInRole(...)-该方法是IPrincipal的一部分 2)ApplicationContext.User.Identity.Name-名称是IIdentity的属性,它是ApplicationUser或IPricipal的一部分
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
我的测试示例(使用RhinoMock):
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
}
}
我对第二个值(身份名称)有轻微的问题.我尝试模拟它,但是在内部完成时很难将模拟的IIdentity分配给ApplicationUser. 有人告诉我我自己创建一些IIPrincipal(包括IIdentity),而不要完全嘲笑它.可以肯定地做到这一点.不确定是否可以使用Stub来调用它?
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?
那么您能建议我如何处理IPrincipal和IIdentity吗?任何建议都非常欢迎.
So can you advice me how to deal with IPrincipal and IIdentity? Any suggestion most welcome.
推荐答案
出现空引用错误的原因是因为IPrincipal.Identity
为空.尚未在您模拟的IPrincipal
中设置它.调用.Name
为空的Identity
会导致您的异常.
The reason you're getting a null reference error is because IPrincipal.Identity
is null; it hasn't been set in your mocked IPrincipal
yet. Calling .Name
the null Identity
results in your exception.
答案是模拟IIdentity
还,并将其设置为为其Name
属性返回"ju".然后,您可以告诉IPrincipal.Identity
返回模拟IIdentity
.
The answer, as Carlton pointed out, is to mock IIdentity
also, and set it up to return "ju" for its Name
property. Then you can tell IPrincipal.Identity
to return the mock IIdentity
.
这是您执行此操作的代码的扩展(使用Rhino Mocks而不是Stubs):
Here is an expansion of your code to do this (using Rhino Mocks rather than Stubs):
public void BeforeTest()
{
mocks = new MockRepository();
IPrincipal mockPrincipal = mocks.CreateMock<IPrincipal>();
IIdentity mockIdentity = mocks.CreateMock<IIdentity>();
ApplicationContext.User = mockPrincipal;
using (mocks.Record())
{
Expect.Call(mockPrincipal.IsInRole(Roles.ROLE_MAN_PERSON)).Return(true);
Expect.Call(mockIdentity.Name).Return("ju");
Expect.Call(mockPrincipal.Identity).Return(mockIdentity);
}
}
这篇关于模拟身份和IPrincipal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!