Moq: Unable to cast to interface 



حمایت از ما | مشهدهاست

Moq: Unable to cast to interface


حمایت از ما | مشهدهاست

حمایت از ما | مشهدهاست حمایت از ما | مشهدهاست

حمایت از ما | مشهدهاست
`

Moq: Unable to cast to interface



asked

6 years ago

viewed

4082 times

active

4 years ago


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.


2010-05-05 21:33:05Z


1:


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.



2:




3:


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?



4:


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?




 اين مطلب بدون دخالت انساني عينا از اين آدرس کپي شده و تمامي مسوليت آن با ناشر اصلي است.

Why is access to the path denied?
Login failed for user 'IIS APPPOOL\ASP.NET v4.0'
How to call javascript function from code-behind
ASP.NET Web Application Message Box
IE9 JavaScript error: SCRIPT5007: Unable to get value of the property 'ui': object is null or undefined
How to Convert DataSet to DataTable

How to add doublequotes to a string that is inside a variable?
Select method in List<t> Collection
Setting dropdownlist selecteditem programmatically
How to write a simple Html.DropDownListFor()?
What is the right way to populate a DropDownList from a database?
A potentially dangerous Request.Path value was detected from the client (*)
*