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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:54:58+00:00 2026-05-27T13:54:58+00:00

I have a solution with the following architecture MVC app > Services > Repositories.

  • 0

I have a solution with the following architecture MVC app > Services > Repositories. Now lets say I have a Document Model and a Document has many Notes. I have a Notes controller with an AddNote(documentID) action. Now my business rules say that only the users associated with the document can add notes so I enforce this. I am currently going with the following method:

public ActionResult AddNote(int documentID)
    {
        try
        {
            NoteCreateEditViewModel viewModel = Mapper.Map<Note, NoteCreateEditViewModel>(noteService.GetNewNote(User.Identity, documentID));
            return View(viewModel);
        }
        catch (BusinessLogicException validationException)
        {
            // Send user somewhere else.
        }
    }

noteService.GetNewNote(User.Identity, documentID) throws a BusinessLogicException should the user have no right on the Document. I am using an exception as this seems to be the best way to handle business/validation violations (although I personally feel this is bad use of exceptions!).

When testing the above from a controller test, should I just be testing that the correct redirect (or whatever) happens should the BusinessLogicException be thrown or should I be simulating an unauthorised user/non existent documentID etc? In my mind, the service test for GetNewNote(User.Identity, documentID) would be the place to simulate an unauthorised user as this ensures my tests are testing the core functionality of a particular piece of code.

However, if I dont write a controller test which simulates an unauthorised user generating an Exception, then how will someone who looks at my tests in the future understand that the desired functionality of AddNote is to prevent unauthorised users from adding notes. If they dont understand this they may be tempted to simply initialise a new Note directly in the controller, rather than through noteService.GetNewNote(User.Identity, documentID).

So should I be testing more in my controllers or is there a much simpler design in adding associated models?

  • 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-27T13:54:58+00:00Added an answer on May 27, 2026 at 1:54 pm

    In your unit test you should mock the noteService.GetNewNote method call so that it throws a BusinessLogicException. Then assert that the controller action returned a redirect result.

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

Sidebar

Related Questions

We have a Asp.net webforms webapplication. The solution has the following structure: MySolution\ MySolution.Main\
Say I have the following solution with multiple versions of the same code each
Let's say we have a solution with the following structure: Project.DAL - Data access
Lets say I have the following entities that map to database tables (every matching
I have the following Solution: SomeProject.Ria (non Silverlight code) SomeProject.Ria.Silverlight (Silverlight light code, namespace
When using ASP.net webforms my usual solution would have following type of setup -
I have a visual studio solution with the following projects: UI DataAccess BusiessLogic BusinessObjects
I have trouble finding a solution for the following problem: I have a lot
I have a VS solution, with the following projects. -GUI -DataAccess -BusinessLogic -BusinessObjects but
gem install heroku failed with following message and I have tried the solution here

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.