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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:59:11+00:00 2026-05-24T06:59:11+00:00

Say I have an MVC Action like: public ActionResult CustomerRecord(customerId) { if (_currentUser.CanViewCustomer(customerId)) return

  • 0

Say I have an MVC Action like:

public ActionResult CustomerRecord(customerId)
{
    if (_currentUser.CanViewCustomer(customerId))
        return View();

    else 
    {
        // user has tried to access an unauthorised record, 
        // should not be here!
        _logger.Log(new SecurityException());
        return View("UnauthorizedAccess");
    }
}

To test the case of an unauthorized access attempt, how many test methods should there be?

i.e. do I write a single test:

CustomerRecord_WithUnauthorizedUser_LogsExceptionAndReturnsUnauthorizedView

or do I write two tests:

CustomerRecord_WithUnauthorizedUser_LogsException
CustomerRecord_WithUnauthorizedUser_ReturnsUnauthorizedView

I guess the problem is that technically the controller violates SRP, but I don’t see that as being a problem in itself (please correct me if you disagree). I’m just not sure how that maps to test methods. One test per responsibility of the method, or one test per single route through the method?

  • 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-24T06:59:12+00:00Added an answer on May 24, 2026 at 6:59 am

    Your controller isn’t necessarily violating SRP by doing two things – it’s still only got one responsibility (controlling).

    In this specific example, I would caution against asserting that a log call is made – it’s not going to affect the functionality of your application if you take out the log statement. Overspecifying unit tests makes them brittle and a pain to maintain, which is part of the reason I like BDD so much.

    If you had to audit every failed attempt then I guess that’s worth unit testing, so if that’s what you’re doing then read on:

    In general, you should only have one assertion (maybe you’d have to call an Assert method or two to make one semantic assertion) per unit test – basically because it’s nice to be able to look at the name of the failed test and to know exactly what’s gone wrong without having to look at the code. So I’d advocate having two tests…

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

Sidebar

Related Questions

Let say I have two methods in MVC 4 Web API controller: public IQueryable<A>
Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
Say you have an MVC view for editing a Sandwich: sandwich name, price, etc.
Let me say I have a route/action for showing content pages like this: /Page/{slug}
Say I have an ASP.Net MVC webiste hosted at www.somedomain.com. Now I am interested
I have an MVC application. Say for example if we have a dropdown Cars
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say I have this method public void test(IList<AvaliableFeaturesVm> vm) { } I have this
Say I have the following generic class: public class Foo<T extends Bar> { //

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.