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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:37:17+00:00 2026-05-15T10:37:17+00:00

I’m doing unit testing for our WCF RIA services, which have RequiresRole or RequiresAuthentication

  • 0

I’m doing unit testing for our WCF RIA services, which have RequiresRole or RequiresAuthentication attributes attached to them. I’ve been able to test the Update, Insert, and Delete methods to ensure the attributes are properly set. This is done by mocking a IServiceProvider, creating a DomainServiceContext with that provider and the correct DomainOperationType, adding an IPrincipal service to the service provider and then running Submit() on the service with an appropriate ChangeSet. This seems to work well.

However, I have been unable to test Query calls. These are called via the Query() method on the service. So I am doing the same prep work as with the others (Creating the IServiceProvider, DomainServiceContext and IPrincipal) and trying to create an appropriate DomainOperationEntry and QueryDescription to pass to Query(). Unfortunately, I’ve not had any luck with this yet. The relevant code is:

string operationName = "GetUsers";
DomainServiceContext domainServiceContext = GetDomainServiceContext(
    authenticate: false,
    operationType: DomainOperationType.Query);
DomainOperationQuery operationQuery = mocks.DynamicMock<DomainOperationEntry>(
    typeof(UserService), operationName, DomainOperation.Query,
    typeof(IQueryable<User>), new List<DomainOperationParameter>(),
    new AttributeCollection());
mocks.ReplayAll();

service.Initialize(domainServiceContext);

int totalCount;
IEnumerable<ValidationResult> validationErrors;
QueryDescription = new QueryDescription(operationEntry);

service.Query(queryDescription, out ValidatoinErrors, out TotalCount);

This should throw an UnauthorizedAccessException, when RequiresAuthentication is set on the GetUsers query. However, I don’t get anything, regardless of whether the attribute is set. Using the debugger with a breakpoint set on the GetUsers method I can see that method is never called. My guess is I’ve got the operationName wrong. But I don’t know whether that’s the problem, or, if it is, what I should change it to.

Does anyone have any insight on this? I’ve searched all through MSDN and done Google searches and searched here extensively. I’ve got nothing so far.

  • 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-15T10:37:17+00:00Added an answer on May 15, 2026 at 10:37 am

    I think there are two things to do when unit testing authorization:

    First, check that the right rules have been applied. You don’t have to execute the rule for this. Reflection tells you if the right rule has been applied. That is a by-product of the fact that rules are declaratively applied. More specifically, you’d use a higher level API above and beyond reflection – DomainServiceDescription against a DomainService type.

    Next, test the rule does what it is supposed to do. For this create a mock implementation of IPrincipal, and an AuthorizationContext, and call the IsAuthorized method of the AuthorizationAttribute (where each attribute corresponds to a rule that you want to unit test).

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.