Moq: cannot pass to interface

Today I asked about this question .

So, since moq creates its own class from the interface, I could not pass it to another class.

So, I was wondering if I would create an ICustomPrincipal and try to do this.

Here's what my layouts look like:

var MockHttpContext = new Mock<HttpContextBase>();
var MockPrincipal = new Mock<ICustomPrincipal>();

MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object);

In the method I'm trying to test, the following code gives an error (again):

var user = (ICustomPrincipal)httpContext.User;

The error is as follows:

Unable to cast object of type 'IPrincipalProxy4081807111564298854aabfc890edcc8' 
to type 'MyProject.Web.ICustomPrincipal'.

I suppose I still need practice with interfaces and moq, but shouldn't I use the class created by moq for ICustomPrincipal? I know that httpContext.User returns an IPrincipal, so maybe something is lost there?

Well, if anyone can help me, I would appreciate it.

Pickels

Edit:
, . , , :

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, . .

+5
4

, mocks ...

, , :

public static HttpContextBase HttpContext;
public static ICustomPrincipal User;

...

var user = (ICustomPrincipal)User;

(, ClassUnderTest)

ClassUnderTest.HttpContextBase = MockHttpContext.Object;

ClassUnderTest.User = MockPrincipal.Object;

... , .

0

, http .

, , , , Mock http , ?

var user = (ICustomPrincipal)httpContext.User;

?

/ ?

+1

, /. , ICustomPrincipal, ?

And why does the error message say "IPrincipalProxy"? Are you kidding the interface IPrincipalsomewhere? Then what is the relationship between IPrincipaland ICustomPrincipal?

0
source

All Articles