起订量:无法投射到接口
今天早些时候,我问了这个问题。
因此,由于起订量从接口创建它自己的类,我无法将其转换为不同的类。
所以这让我想知道如果我创建一个 ICustomPrincipal 并尝试转换为它会怎样。
这就是我的模拟的样子:
var MockHttpContext = new Mock<HttpContextBase>();
var MockPrincipal = new Mock<ICustomPrincipal>();
MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object);
在我尝试测试的方法中,以下代码再次给出错误:
var user = (ICustomPrincipal)httpContext.User;
错误如下:
Unable to cast object of type 'IPrincipalProxy4081807111564298854aabfc890edcc8'
to type 'MyProject.Web.ICustomPrincipal'.
我想我仍然需要一些接口和起订量的练习,但我不应该能够将 moq 创建的类强制转换回 ICustomPrincipal?我知道 httpContext.User 返回一个 IPrincipal 所以也许那里丢失了一些东西?
好吧,如果有人可以帮助我,我将不胜感激。
Pickels
编辑:
根据要求,我正在测试的方法的完整代码。它还没有完成,但这就是我到目前为止所拥有的:
public bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
var user = (ICustomPrincipal)httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
}
return true;
}
Edit2:
似乎如果我使用 Thread.CurrentPrincipal 而不是 HttpContext.current.user 我可以毫无问题地转换它。现在就来了解一下两者之间的差异。
earlier today I asked this question.
So since moq creates it's own class from an interface I wasn't able to cast it to a different class.
So it got me wondering what if I created a ICustomPrincipal and tried to cast to that.
This is how my mocks look:
var MockHttpContext = new Mock<HttpContextBase>();
var MockPrincipal = new Mock<ICustomPrincipal>();
MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object);
In the method I am trying to test the follow code gives the error(again):
var user = (ICustomPrincipal)httpContext.User;
The error is the following:
Unable to cast object of type 'IPrincipalProxy4081807111564298854aabfc890edcc8'
to type 'MyProject.Web.ICustomPrincipal'.
I guess I still need some practice with interfaces and moq but shouldn't I be able to cast the class that moq created back to ICustomPrincipal? I know httpContext.User returns an IPrincipal so maybe something gets lost there?
Well if anybody can help me I would appreciate that.
Pickels
Edit:
As requested the full code of the method I am testing. It's still not finished but this is what I have so far:
public bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
var user = (ICustomPrincipal)httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
}
return true;
}
Edit2:
Seems that if I use Thread.CurrentPrincipal instead of HttpContext.current.user I can cast it without a problem. Reading up on the differences between the two now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 .As() 进行投射,请参阅 http://code.google.com/p /moq/wiki/QuickStart#Advanced_Features
Cast using .As(), see http://code.google.com/p/moq/wiki/QuickStart#Advanced_Features
您的代码示例表明您正在模拟 http 上下文和主体对象。
但是,您尝试让用户获取的代码示例很难确定您使用的是 Mock http 上下文还是框架提供的上下文?
上面的行是否在使用依赖注入的方法或对象中使用?
您能向我展示整个方法/对象吗?
Your code example show that you are mocking a http context and principal objects.
However, your code example in which you are trying to get the user it is hard to determine if you are using the Mock http context or one provided by the framework?
Is the above line used in a method or object using dependency injection?
Are you able to show me the method / object as a whole?
我认为你需要能够将模拟注入到你的代码中...
例如,在你的类中,如果你添加以下内容:
并在你的代码中包含以下内容...
以及在你的测试类中(假设它被命名为ClassUnderTest)
以及
...我认为这应该可以为您解决问题。
I think you need to be able to inject your mocks into your code...
For instance, in your class if you add the following:
and have the following in your code...
and in your class under test (say it is named ClassUnderTest)
and
well... I think that that should fix things for you.
我猜您对命名空间/接口名称有些困惑。
您确定要转换为您创建模拟的同一个
ICustomPrincipal
吗?为什么错误消息中显示“IPrincipalProxy”?您是否在某处嘲笑
IPrincipal
接口?那么IPrincipal
和ICustomPrincipal
之间是什么关系呢?I'm guessing you have some confusion in namespaces / interface names.
Are you sure, you're casting to the same
ICustomPrincipal
, for which you create your mock?And why is it saying "IPrincipalProxy" in the error message? Are you mocking
IPrincipal
interface somewhere? Then what's the relation betweenIPrincipal
andICustomPrincipal
?