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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:16:05+00:00 2026-05-11T22:16:05+00:00

I am not sure what I should be doing here. Should I be hardcoding

  • 0

I am not sure what I should be doing here. Should I be hardcoding all the values in or should I have them in a CONST variables. Everything I seen seems to hard code the values in so I am not sure.

Like this is what I was doing now.

Say in my controller I had a validation test to check if the user tries to submit a form with a blank field.

Now I would have a if statement checking for blank or null variable. If this happened then I would add the error to the ModelState with an error message that I wrote.

so in my unit test I want to make sure that if a blank form variable is submitted that it gets caught.

now in my unit testing I just made a CONST varible and copied in and pasted the validation message in.

So in my assert I compare what the actual message is compared to the message stored in my CONST Varrible. I do this by calling like the Model state and call the field up where I expect the error to be.

Like:

result.ViewData.ModelState[“username”].Errors[0];

So if the message is there then it must have gone into my code otherwise it would not exist.

So it occurred to me maybe I should make a new class that will be static and hold all these CONST variables.

That way both the controller views and the unit tests can use them. That way if I got to change the error message then I only need to change it one place. Since I am not testing what the error message is I am testing if it gets set.

The same thing say for exceptions I have some custom messages but I am not testing if the message is right, more if the expection got caught.

The way I am testing it though is to see if the message is the message that I expect since if it is not the message or the message does not exist then something went wrong.

I am new to unit testing so I wanted to make sure that what I am going to do won’t some how screw up my unit tests.

To me it makes sense but I thought better check first.

Thanks

  • 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-11T22:16:06+00:00Added an answer on May 11, 2026 at 10:16 pm

    It’s important to write each test in a way that is robust to subsequent change. You will often need to change parts of your application at a later date, and each time you do that, there’s a risk that you will break one of more of your tests.

    If your tests are robust to change, a failing test will truly indicate a regression bug.

    However, if your tests are what’s called Overspecified Tests, every little change you make in your code base may cause tests to fail – not because there was a regression bug, but because the test is too brittle. When this happens, you lose faith in your tests; test maintainance takes a lot of time, and you’ll eventually end up abandoning the test suite altogether.

    As I read your question, you are already beginning to see the contours of this anti-pattern. I assume that is why you don’t test for the specific texts returned, but merely whether they are being set at all. I think this is correct – I rarely test for specific strings, but rather whether a string is specified at all. This makes the test more robust to change, and you avoid the Overspecified Test anti-pattern.

    In many cases, instead of doing an Assert.AreEqual on two strings, you can just use Assert.IsNotNull, or perhaps Assert.IsFalse(string.IsNullOrEmpty(result)) (your platform seems to be .NET).

    In general, assertions based on Derived Values are very robust to change, so you may want to take a look at the following blog post:

    • Derived Values Ensure Executable Specification

    If you feel particularly adventurous, I can only recommend that you read xUnit Test Patterns, from where many of the patterns and anti-patterns I mention originate. The Art of Unit Testing is also good…

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

Sidebar

Ask A Question

Stats

  • Questions 177k
  • Answers 177k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One way is to add your shortcut keys to the… May 12, 2026 at 3:23 pm
  • Editorial Team
    Editorial Team added an answer If you find yourself querying across databases, you should look… May 12, 2026 at 3:23 pm
  • Editorial Team
    Editorial Team added an answer What you're looking for would not be an XML parser.… May 12, 2026 at 3:23 pm

Related Questions

I am in the midst of updating data in multiple tables. Currently I have
I am not sure how to phrase a good question, so I will just
I have a project where I am taking some particularly ugly live HTML and
I am using polymorphic associations to track Comments in my project. All very straight

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.