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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:53:46+00:00 2026-06-13T15:53:46+00:00

In an MVC4 C# website, we are using Unit Testing to test our code.

  • 0

In an MVC4 C# website, we are using Unit Testing to test our code. What we are currently doing is creating a mock database, then testing each layer:

  1. Test the Database – connecting, initializing, load, etc.
  2. Test the code accessing the database
  3. Test the view layer

I want these three categories to be run in order and only if the previous one passed. Is this the right way to go about it? The old way we were doing it was using a Dependency Injection framework was a weird approach to mock testing. We actually created a mock class for each data accessor, which sucks because we have to re implement the logic of each method, and it didn’t really add any value because if we made a mistake with the logic the first time, we’ll make it a second.

  • 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-06-13T15:53:48+00:00Added an answer on June 13, 2026 at 3:53 pm

    I want these three categories to be run in order and only if the
    previous one passed. Is this the right way to go about it?

    No. You should be test everything in isolation and because of that any failing data access code should be completely unrelated to the view. So the view unit tests should fail for a completely different reason and because of that you should always run all tests.

    In general, tests should run in isolation, and should not depend on any other tests. It seems to me that the view layer still depends on the data access code, but that you only abstracted the layer below. In that case, from the view’s perspective, you are mocking an indirect dependency, which makes your unit tests more complex than strictly needed.

    The old way we were doing it was using a Dependency Injection
    framework was a weird approach to mock testing

    I’m not sure if I get this, but it seems as if you were using the DI container inside your unit tests. This is a absolute no-no. Don’t clutter your tests with any DI framework. And there’s no need to. Just design your classes around the dependency injection principle (expose all dependencies as constructor arguments) and in your test you just manually new up the class under test with fake dependencies -or- when this leads to repetitive code, create a factory method in your test class that creates a new instance of the class under test.

    We actually created a mock class for each data accessor, which sucks

    I’m not sure, but it sounds as if there’s something wrong with your design here. You would normally have a IRepository<T> interface for which you would have multiple implementations, such as a UserRepository : IRepository<User>, and OrderRepository : IRepository<Order>. In your test suite, you will have one generic FakeRespository<T> or InMemoryRepository<T> and you’re done. No need to mock many classes here.

    Here are two great books you should definitely read:

    1. The Art of Unit Testing
    2. Dependency Injection in .NET

    Those books will change your life. And since you’ll be reading anyway, do read this book as well (not related to your question, but code be a life changer as well).

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

Sidebar

Related Questions

I created a website using ASPNET.MVC4 RC. For each view I created 2 files
I'm am making a model first website using MVC4 on IIS7. Currently my connection
I have javascript code like this: <a href=javascript:window.history.back()... When I test my website (using
I am using VS2012 and having some troubles publishing an mvc4 website. None of
Planning to create a new website for our product using ASP.NET MVC 4. Site
I am creating an asp.net mvc4 site using entity framework 5 with codefirst and
I'm optimizing my ASP.net MVC 4 website. I'm currently using run time bundling to
I'm writing a MVC4 Website using Microsoft Visual Studio express 2012 for web. Whenever
I'm using the aspnet_membership with an MVC4 website project. This is just for a
I'm using ASP.NET MVC 4 for a website, and one of the things I'm

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.