Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Unit testing overkill? (TDD)

So I'm starting to catch the TDD bug but I'm wondering if I'm really doing it right... I seem to be writing A LOT of tests.

The more tests the better, sure, but I've got a feeling that I'm over doing it. And to be honest, I don't know how long I can keep up writing these simple repetitive tests.

For instance, these are the LogOn actions from my AccountController:

public ActionResult LogOn(string returnUrl)
{
    if (string.IsNullOrEmpty(returnUrl))
        returnUrl = "/";

    var viewModel = new LogOnForm()
    {
        ReturnUrl = returnUrl
    };

    return View("LogOn", viewModel);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(LogOnForm logOnForm)
{
    try
    {
        if (ModelState.IsValid)
        {
            AccountService.LogOnValidate(logOnForm);

            FormsAuth.SignIn(logOnForm.Email, logOnForm.RememberMe);

            return Redirect(logOnForm.ReturnUrl);
        }
    }
    catch (DomainServiceException ex)
    {
        ex.BindToModelState(ModelState);
    }
    catch
    {
        ModelState.AddModelError("*", "There was server error trying to log on, try again. If your problem persists, please contact us.");
    }

    return View("LogOn", logOnForm);
}

Pretty self explanatory.

I then have the following suite of tests

public void LogOn_Default_ReturnsLogOnView()
public void LogOn_Default_SetsViewDataModel()
public void LogOn_ReturnUrlPassedIn_ViewDataReturnUrlSet()
public void LogOn_ReturnUrlNotPassedIn_ViewDataReturnUrDefaults()
public void LogOnPost_InvalidBinding_ReturnsLogOnViewWithInvalidModelState()
public void LogOnPost_InvalidBinding_DoesntCallAccountServiceLogOnValidate()
public void LogOnPost_ValidBinding_CallsAccountServiceLogOnValidate()
public void LogOnPost_ValidBindingButAccountServiceThrows_ReturnsLogOnViewWithInvalidModelState()
public void LogOnPost_ValidBindingButAccountServiceThrows_DoesntCallFormsAuthServiceSignIn()
public void LogOnPost_ValidBindingAndValidModelButFormsAuthThrows_ReturnsLogOnViewWithInvalidModelState()
public void LogOnPost_ValidBindingAndValidModel_CallsFormsAuthServiceSignIn()
public void LogOnPost_ValidBindingAndValidModel_RedirectsToReturnUrl()

Is that over kill? I haven't even shown the services tests!

Which ones (if any) can I cull?

TIA,
Charles

like image 444
Charlino Avatar asked Sep 03 '09 06:09

Charlino


People also ask

Is unit testing the same as TDD?

“Unit testing” is writing many small tests that each test one very simple function or object behavior. TDD is a thinking process that results in unit tests, and “thinking in tests” tends to result in more fine-grained and comprehensive testing, and an easier-to-extend software design.

What is TDD in ASP NET MVC?

What is TDD? Test-Driven Development is a software development process being converted to the test cases before the application is fully developed. The application should be in a loosely coupled manner so that we can test the independent part of the application. Let's start step by step testable application in MVC.

What are commonly used tool for unit testing in ASP NET MVC?

To write unit tests, you need a unit testing framework. Some popular ones are MSTest that comes with Visual Studio, NUnit, and XUnit. There are others. I will use NUnit in my examples.

Does unit testing speed up development?

Unit testing works best in conjunction with integration testing, but there are unique benefits from unit testing. This includes faster development because typically you write the unit test even before you write the code and then test your code against said test.


1 Answers

It all depends on how much coverage you need / want and how much dependability is an issue.

Here are the questions you should ask yourself:

  • Does this unit test help implement a feature / code change that I don't already have?
  • Will this unit test help regression test/debug this unit if I make changes later?
  • Is the code to satisfy this unit test non-trivial or does it deserve a unit test?

Regarding the 3rd one, I remember when I started writing unit tests (I know, not the same thing as TDD) I would have tests that would go like:

string expected, actual;
TypeUnderTest target = new TypeUnderTest();
target.PropertyToTest = expected;
actual = target.PropertyToTest;
Assert.AreEqual<string>(expected, actual);

I could have done something more productive with my time like choose a better wallpaper for my desktop.

I recommend this article by ASP.net MVC book author Sanderson:

http://blog.codeville.net/2009/08/24/writing-great-unit-tests-best-and-worst-practises/

like image 181
TJB Avatar answered Nov 16 '22 02:11

TJB