Mocking FormsIdentity.Ticket.UserData with Moq

As part of the unit test, I'm trying to mock the return value of FormsIdentity.Ticket.UserData p>

The following will not work, but it should give an idea of ​​what I'm trying to do:

var principal = Mock<IPrincipal>(); var formsIdentity = Mock<FormsIdentity>(); formsIdentity.Setup(a => a.Ticket.UserData).Returns("aaa | bbb | ccc"); principal.Setup(b => b.Identity).Returns(formsIdentity.Object); 

The code I'm trying to check looks something like this:

 FormsIdentity fIdentity = HttpContext.Current.User.Identity as FormsIdentity; string userData = fIdentity.Ticket.UserData; 

All I want to do in my unit test is fake the return value from FormsIdentity.Ticket.UserData. But when I run the code in the first section, I get an error when trying to mock FormsIdentity. The error says that the type for bullying should be an interface, an abstract class, or an unsealed class.

I tried to use the IIdentity parameter instead of FormsIdentity (FormsIdentity is an implementation of IIdentity), but IIdentity does not have .Ticket.UserData.

So how can I write this test to get the value from FormsIdentity.Ticket.UserData?

+6
tdd asp.net-mvc moq mocking
source share
1 answer

I'm not a Unit Test expert, in any way, just wet feet in the area.

Isn't it unnecessary to mock Identity in unit test, because Identity code is code that can be considered work already in isolation? (i.e. is this Microsoft code?) For example, when unit testing your own code, you won’t need to mock one of the Framework objects. I mean, have you ever needed to make fun of a list or dictionary?

If you really want to test your code in isolation or for some reason have super-fine control over the data returned to Userdata, can you just write an interface for the interaction between Identity and your code?

 Public Interface IIdentityUserData Readonly Property UserData As String End Interface Public Class RealIdentityWrapper Implements IIdentityUserData Private _identity as FormsIdentity Public Sub New(identity as FormsIdentity) 'the real version takes in the actual forms identity object _identity = identity End Sub Readonly Property UserData As String Implements IIDentityUserData.UserData If not _identity is nothing then Return _identity.Ticket.UserData End If End Property End Class 'FAKE CLASS...use this instead of Mock Public Class FakeIdentityWrapper Implements IIdentityUserData Readonly Property UserData As String Implements IIDentityUserData.UserData If not _identity is nothing then Return "whatever string you want" End If End Property End Class 'here the code that you're trying to test...modified slightly Dim fIdentity As FormsIdentity= HttpContext.Current.User.Identity Dim identityUserData As IIdentityUserData identityUserData = 'TODO: Either the Real or Fake implementation. If testing, inject the Fake implementation. If in production, inject the Real implementation Dim userData as String userData = identityUserData.UserData 

Hope this helps

0
source share

All Articles