起订量:无法投射到接口

发布于 2024-08-31 14:58:54 字数 1371 浏览 3 评论 0原文

今天早些时候,我问了这个问题

因此,由于起订量从接口创建它自己的类,我无法将其转换为不同的类。

所以这让我想知道如果我创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海里的那抹蓝 2024-09-07 14:58:54

您的代码示例表明您正在模拟 http 上下文和主体对象。

但是,您尝试让用户获取的代码示例很难确定您使用的是 Mock http 上下文还是框架提供的上下文?

var user = (ICustomPrincipal)httpContext.User;

上面的行是否在使用依赖注入的方法或对象中使用?

您能向我展示整个方法/对象吗?

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?

var user = (ICustomPrincipal)httpContext.User;

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?

凉城凉梦凉人心 2024-09-07 14:58:54

我认为你需要能够将模拟注入到你的代码中...

例如,在你的类中,如果你添加以下内容:

public static HttpContextBase HttpContext;
public static ICustomPrincipal User;

并在你的代码中包含以下内容...

var user = (ICustomPrincipal)User;

以及在你的测试类中(假设它被命名为ClassUnderTest)

ClassUnderTest.HttpContextBase = MockHttpContext.Object;

以及

ClassUnderTest.User = MockPrincipal.Object;

...我认为这应该可以为您解决问题。

I think you need to be able to inject your mocks into your code...

For instance, in your class if you add the following:

public static HttpContextBase HttpContext;
public static ICustomPrincipal User;

and have the following in your code...

var user = (ICustomPrincipal)User;

and in your class under test (say it is named ClassUnderTest)

ClassUnderTest.HttpContextBase = MockHttpContext.Object;

and

ClassUnderTest.User = MockPrincipal.Object;

well... I think that that should fix things for you.

素染倾城色 2024-09-07 14:58:54

我猜您对命名空间/接口名称有些困惑。
您确定要转换为您创建模拟的同一个 ICustomPrincipal 吗?

为什么错误消息中显示“IPrincipalProxy”?您是否在某处嘲笑 IPrincipal 接口?那么IPrincipalICustomPrincipal之间是什么关系呢?

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 between IPrincipal and ICustomPrincipal?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文