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 610905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:43:34+00:00 2026-05-13T17:43:34+00:00

I am using Moq for unit testing and I would like to test for

  • 0

I am using Moq for unit testing and I would like to test for a view’s attribute.
In this case the Authorize attribute.

Example View Code:

[Authorize(Roles = "UserAdmin")]
public virtual ActionResult AddUser()
{
   // view logic here  
   return View();
}

So I would like to test the view attribute when I act on this view with a user that is in the role of UserAdmin and a user that is not in the role of user admin. Is there anyway to do this ?

Example Test:

[Test]
public void Index_IsInRole_Customer()
{
   // Arrange
   UserAdminController controller = _controller;
   rolesService.Setup(r => r.IsUserInRole(It.IsAny<string>(), It.IsAny<string>())).Returns(false); // return false for any role

   // Act
   var result = controller.AddUser();

   // Assert
   Assert.IsNotNull(result, "Result is null");
}
  • 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-13T17:43:35+00:00Added an answer on May 13, 2026 at 5:43 pm

    Attributes are just metadata on the type, so they don’t do anything unless the surrounding infrastructure make them do something (or better yet: the surrounding infrastructure does something based on the information in those attributes). That’s what the ASP.NET MVC framework does when it executes a request.

    That is not what you do when you create and invoke a Controller Action in a unit test, so unless you want to go to great lengths to invoke the Controller Action using a ControllerActionInvoker (at which point the test ceases to be a unit test and becomes an integration test) you can’t directly test the behavior implied by the attribute.

    You can, however, write a unit test that verifies that the attribute correctly decorates the Controller Action:

    var attributes = typeof(UserAdminController)
        .GetMethod("AddUser").GetCustomAttributes(true);
    var result = attributes.OfType<AuthorizeAttribute>().Single();
    Assert.AreEqual("UserAdmin", result.Roles);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the Moq framework for unit testing and would like to be
I'm doing some unit testing, and mocking some properties using Moq . Now, this
I'm using Microsoft's Visual Studio Test Tools and Moq for unit testing. I have
For unit testing, I'm using NUnit 2.6 and Moq 4.0. There's a particular case
In my unit-tests I'm mocking a protected method using Moq, and would like to
We are using Moq to unit test our service classes, but are stuck on
I have been writing unit tests using NUnit and Moq with my Silverlight code
Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
I am using Moq for unit testing, and I am trying to write my
I'm having a bit of trouble doing some unit-testing using moq. If I have

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.