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

  • Home
  • SEARCH
  • 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 6548333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:56:19+00:00 2026-05-25T11:56:19+00:00

Here’s my example: [TestMethod] public void NewAction_should_return_IndexAction() { NewViewModel viewModel = new NewViewModel() {

  • 0

Here’s my example:

[TestMethod]
public void NewAction_should_return_IndexAction()
{
    NewViewModel viewModel = new NewViewModel()
    {
        Name = "José Inácio Santos Silva",
        Email = "joseinacio@joseinacio.com",
        Username = "joseinacio"
    };

    //IsUserRegistered is used to validate Username, Username is unique.
    _mockAuthenticationService.Setup(x => x.IsUserRegistered(viewModel.Username )).Returns(false);

    //IsUserRegistered is used to validate Email, Email is unique.
    _mockUsuarioRepository.Setup(x => x.GetUserByEmail(viewModel.Email));
    _mockDbContext.Setup(x => x.SaveChanges());
    _mockUsuarioRepository.Setup(x => x.Add(It.IsAny<User>()));

    _userController = new UserController(_mockUsuarioRepository.Object, _mockDbContext.Object, _mockAuthenticationService.Object);

    ActionResult result = _userController.New(viewModel);

    result.AssertActionRedirect().ToAction("Index");

    _mockAuthenticationService.VerifyAll();
    _mockUsuarioRepository.VerifyAll();
    _mockDbContext.VerifyAll();
}

I have read some tutorials and they say that we should use only one mock per test.

But look at my test, it use 3 mocks, to check if my Action is working the right way I need to check these 3 mocks, do not agree?

How do I make this test in the correct way?

  • 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-25T11:56:19+00:00Added an answer on May 25, 2026 at 11:56 am

    Each unit test should test only one thing.

    In your unit test you are testing three mock objects. If the mockAuthenticationService fails, this will be reported and the unit test will stop there. Any errors with the other Mock objects are not reported and are effectively hidden.

    In this situation you should create three unit tests, and in each one verify only one of the Mock objects. The rest are just used as stubs. (A stub is exactly the same as a Mock object, except you dont call VerifyAll on it at the end)

    To avoid duplication and wasted effort, you should refactor that unit test so that most of the code is in a separate method. Each of the three unit tests calls this method and then verifies a single Mock.

    You also have a test to ensure the correct redirect is called. This should also be in a separate test.

    Quite simply:

    [TestMethod]
    public void NewAction_should_checkUserRegistered()
    {
        SetupTest();
        _mockAuthenticationService.VerifyAll();
    }
    
    [TestMethod]
    public void NewAction_should_GetUserByEmail()
    {
        SetupTest();
        _mockUsuarioRepository.VerifyAll();
    }
    
    [TestMethod]
    public void NewAction_should_SaveDBContext()
    {
        SetupTest();
        _mockDbContext.VerifyAll();
    }
    
    [TestMethod]
    public void NewAction_should_return_Redirects_Action()
    {
        var novoActionResult = SetupTest();
        novoActionResult.AssertActionRedirect().ToAction("Index");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here is what I have: Dim cmsManager As New Telerik.Cms.CmsManager() Dim currentNode As Telerik.Cms.Web.CmsSiteMapNode
Here's a good example: I'm trying to overload OpenGL's glutMouseFunc so it may accept
Here's the latest JSFiddle . HTML: <table id=math-table> <tr> <td><input type=text id=A1 name=A value=></td>
Here is the test code that I am using : public class IOConnectDirect extends
here is the example edited by crop grid: 1st i view like a normal
here's the code: #include <string> class Config { public: static const std::string asdf =
Here is a snippet of my xaml: <ComboBox x:Name=cbo1 Width=100 SelectedValue=200> <ComboBoxItem Name=n1>100</ComboBoxItem> <ComboBoxItem
Here is an example: I have a file 1.js, which has some functions. I
Here is an example UPDATE duration = datediff(ss, statustime, getdate()), statustime = getdate() where

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.