Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 403729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:14:36+00:00 2026-05-12T17:14:36+00:00

As part of a unit test I am trying to mock the return value

  • 0

As part of a unit test I am trying to mock the return value of FormsIdentity.Ticket.UserData

The following will NOT work but it should give an idea of what I am 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 am trying to test 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 the FormsIdentity. The error says the type to mock must be an interface, abstract class or non-sealed class.

I tried to use IIdentity instead of FormsIdentity (FormsIdentity is an implementation of IIdentity) but IIdentity doesn’t have .Ticket.UserData.

So how can I write this test so that I get a value from FormsIdentity.Ticket.UserData?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:14:36+00:00Added an answer on May 12, 2026 at 5:14 pm

    I’m not a Unit Test expert, by any means, just getting my feet wet in the area.

    Isn’t it overkill to mock out the Identity in a unit test, because the Identity code is code that you can assume works already in isolation? (ie. it’s Microsoft’s code?) For example, when unit testing your own code, you wouldn’t need to mock out one of the Framework objects. I mean, would you ever need to mock a List or a Dictionary?

    That being said, if you REALLY want to test your code in isolation or for some reason have super fine control over the data returned in Userdata, can’t you just write an Interface for the interaction between the 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's 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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to unit test some part of my code, I need a user
As part of a unit test framework, I'm writing a function genArray that will
While trying to migrate, I keep getting this error: rake aborted! test-unit is not
Im trying to unit test a class in some Java code that I've inherited.
I am trying to use Gallio (v3.1)/MbUnit/NCover to run a unit test in my
I'm using PHPUnit to try to unit test some PHP files that are part
I would like to perform an audit as part of a unit test that
I have the following code which is part of unit testing for serialization support
As the title says, I'm trying to understand how to unit test (or maybe
I am trying to create a unit test framework using CPPUnit for a large

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.