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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:00:21+00:00 2026-06-10T22:00:21+00:00

I have 2 concerns about my unit test method: Do I test too much

  • 0

I have 2 concerns about my unit test method:

  1. Do I test too much in one test method?
  2. How can my test method name reflect all test expectations?

I asked myself when my method name says: ReturnInvalidModelState, then my 2 other Asserts are not correct. At least concerning the method name…

[Test]
public void Create_TemplateAlreadyExists_ReturnInvalidModelState()
{
    // ARRANGE
    TemplateViewModel templateViewModel = new TemplateViewModel { 
        Name = "MyTest" 
    };

    Mock<ITemplateDataProvider> mock1 = new Mock<ITemplateDataProvider>();
    Mock<IMappingEngine> mock2 = new Mock<IMappingEngine>();

    TemplateController controller = 
        new TemplateController(mock1.Object, mock2.Object);
    mock1.Setup(m => m.TemplateExists("MyTest")).Returns(true);
    // Set ModelState.IsValid to false
    controller.ModelState.AddModelError("Name", 
                                        "This name already exists.");

    // ACT
    ActionResult result = controller.Create(templateViewModel);

    // ASSERT
    Assert.IsFalse(controller.ModelState.IsValid);
    Assert.IsInstanceOfType(typeof(PartialViewResult), result);
    Assert.AreEqual(templateViewModel, ((PartialViewResult)result).Model);
}

[HttpPost]
public ActionResult Create(TemplateViewModel templateViewModel)
{
    if (ModelState.IsValid
        && !_templateDataProvider.TemplateExists(templateViewModel.Name))
    {

        Template template = 
            Mapper.Map<TemplateViewModel, Template>(templateViewModel);

        _templateDataProvider.AddTemplate(template);
        return new JsonNetResult(new { success = true });
    }
    ModelState.AddModelError("Name", "This name already exists.");
    return PartialView(templateViewModel);
}
  • 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-10T22:00:23+00:00Added an answer on June 10, 2026 at 10:00 pm

    Yes, I think that you are testing for too many things.

    Start with renaming your test method. Your method signature should describe action, scenario and expected outcome.

    If I were to rename your method, than I would end up with the following:

    public void Create_DuplicateTemplate_ModelStateIsInvalidAndReturnsPartialViewResultAndPartialViewResultOfTypeTemplateViewModel() 
    { 
    }
    

    Your test is concerned with three things, rather than one. When it fails, you won’t know straight away why it has failed.

    Consider re-factoring this into smaller tests and encapsulating some of the arrangement logic so that it can be re-used.

    Edit:

    You made a good point in your comment regarding single test method having a single assertion. I agree with you on that one, as good as it sounds, often it’s not sufficient.

    Say I have the following action method:

    [HttpPost]
    public ActionResult Register(NewUserViewModel newUser)
    {
        if (!ModelState.IsValid)
            return View(newUser);
    
        var newUserDTO = Mapper.Map<NewUserViewModel, NewUserDTO>(newUser);
        var userDTO = UserManagementService.RegisterUser(newUserDTO);
    
        var result = Mapper.Map<UserDTO, UserViewModel>(userDTO);
    
        TempData.Add("RegisteredUser", result);
        return RedirectToAction("RegisterSuccess");
    }
    

    I have the following unit test for this method:

            [TestMethod]
            public void Register_HttpPost_ValidViewModel_ReturnsRegisterSuccess()
            {
                // Arrange
                var autoMapperMock = this.mockRepository.DynamicMock<IMapper>();
                var userManagementServiceMock = this.mockRepository.DynamicMock<IUserManagementService>();
    
                var invalidRegistrationViewModel = new NewUserViewModel
                {
                    LastName = "Lastname",
                    FirstName = "Firstname",
                    Username = null
                };
    
                autoMapperMock.Expect(a => a.Map<UserDTO, UserViewModel>(Arg<UserDTO>.Is.Anything)).Repeat.Once().Return(null);
                autoMapperMock.Expect(a => a.Map<NewUserViewModel, NewUserDTO>(Arg<NewUserViewModel>.Is.Anything)).Repeat.Once().Return(null);
                userManagementServiceMock.Expect(s => s.RegisterUser(Arg<NewUserDTO>.Is.Anything)).Repeat.Once();
    
                autoMapperMock.Replay();
    
                var controller = new AccountController
                {
                    Mapper = autoMapperMock,
                    UserManagementService = userManagementServiceMock
                };
    
                this.mockRepository.ReplayAll();
    
                // Act
                var result = (RedirectToRouteResult)controller.Register(invalidRegistrationViewModel);
    
                // Assert
                Assert.IsTrue((string)result.RouteValues["Action"] == "RegisterSuccess");
            }
    

    As you can see, I set up multiple expectations on my mock:

    • I expect AutoMapper to be called twice
    • I expect UserManagementService to be called once

    At the end of the test I have a single assertion that checks whether user was re-directed to the correct route.

    So where do I check my assertions? I create another method that makes sure that my expectations have been met:

        [TestCleanup]
        public void Cleanup()
        {
            try
            {
                this.mockRepository.VerifyAll();
            }
            finally
            {                
            }
    }
    

    So you are right, I have three assertions instead of one, but I structure my code in such a way so it appears that I have only one assertion.

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

Sidebar

Related Questions

We're building a multi-language Drupal stack and one of the concerns we have is
I have some concerns about the Integration of Magento with other CRM,ERP systems. How
Im building an app with a tab-layout. And i have some concerns about tab-layout
I love the flexibility of named branches but I have some concerns about the
I have concerns about the safe use of the function str_replace . E.G. $var
I have some concerns related to the fact of testing some functions containing the
I have a couple of concerns, I'm busy building a normal .mobi site for
How can I write unit tests for existing and already implemented code which has
my question concerns about Rails + HAML I would like to conditionally set a
I'm still learning about DDD and I have these two (probably simple) questions: If

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.