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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:21:14+00:00 2026-05-16T10:21:14+00:00

It seems like every unit test example I’ve encountered is incredibly obvious and canned.

  • 0

It seems like every unit test example I’ve encountered is incredibly obvious and canned. Things like assert that x + 3 == 8, and whatnot. I just have a hard time seeing how I’d unit test real world things, like SQL queries, or if a regEx used for form validation is actually working properly.

Case in point: I’m working on two ASP.NET MVC 2 sites that are DB driven. I have a test unit solution for each of them, but have no idea what kind of tests would be useful. Most of the work the site will do is writing data to, or retrieving and organizing data from the DB. Would I simply test that the various queries successfully accessed the DB? How would I test for correctness (e.g., data being written to the proper fields, or correct data being retrieved)?

I’m just having a hard time transforming my own informal manner of testing and debugging into the more formalized, assert(x) kind of testing.

  • 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-16T10:21:15+00:00Added an answer on May 16, 2026 at 10:21 am

    First, ask yourself “Why are unit tests hard to write for my real code?” Perhaps the answer is that your real code is doing too much. If you have a single module of code filled with “new” statements and “if” statements and “switch” statements and clever math statements and database access, it’s going to be painful to write one test, let alone adequately test the logic and the math. But if you pulled the “new” statements out into a factory method, you could get easily provide mock objects to test with. If you pulled the “if” clauses and “switch” statements out into state machine patterns, you wouldn’t have so many combinations to test. If you remove the database access to an external data provider object, you can provide simple test data to execute your math statements. Now you’re testing object creation, state transitions, and data access all separately from your clever math statements. All of these steps got easier by simplifying them.

    A key reason code is hard to test is that it contains “internal dependencies”, such as dependencies that it creates, or dependencies on libraries. If your code says “Foo theFoo = new Foo();” you can’t easily substitute a MockFoo to test with. But if your constructor or method asks for theFoo to be passed in instead of constructing itself, your test harness can easily pass in a MockFoo.

    When you write code, ask yourself “how can I write a unit test for this code?” If the answer is “it’s hard”, you might consider altering your code to make it easier to test. What this does is it makes your unit test the first actual consumer of your code – you’re testing the interface to your code by writing the test.

    By altering your interfaces to make them easier to test, you will find yourself better adhering to the object oriented principles of “tight cohesion” and “loose coupling”.

    Unit testing isn’t just about the tests. Writing unit tests actually improves your designs. Get a little further down the path, and you end up with Test Driven Development.

    Good luck!

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

Sidebar

Related Questions

It seems like every example I can find has to do with using DirectX
It seems like every time I float two elements to the right, they get
Seriously, it seems like every time I want to make my UI elements talk
This seems like it should be something very easy to do, but every time
Seems like there should be... Right now it just seems like magic that you
Seems like this should be obvious, but how do I send arrow key presses
I'm looking for some advice for unit-testing in a manner that doesn't leave test
I'm a bit new to ruby and test/unit so this may seem like a
Seems like every C# static analyzer wants to complain when it sees a public
It seems like every time I update my provisioning profile, my xcode project still

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.