views:

44

answers:

2

Hello

Recently I've started to write on asp.net mvc framework. I have a problem. I can't understand meaning of the unit tests. Let's see on example

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
        ViewData["Message"] = "Welcome to ASP.NET MVC!"; 
        return View(); 
    } 

    public ActionResult About() 
    { 
        return View(); 
    } 
} 

and the tests

[TestMethod] 
public void Index() 
{ 
    HomeController controller = new HomeController(); 

    ViewResult result = controller.Index() as ViewResult; 

    ViewDataDictionary viewData = result.ViewData; 
    Assert.AreEqual("Welcome to ASP.NET MVC!", viewData["Message"]); 
} 

[TestMethod] 
public void About() 
{ 
    HomeController controller = new HomeController(); 

    ViewResult result = controller.About() as ViewResult; 

    Assert.IsNotNull(result); 
} 

I have a questions.

1) In what case, method About won't return View?

If there is no view About, method will return nothing. It' obviously It is simple to click "Run", type in browser "Home/About" and see result. Without any unit tests. It's more faster than creating unit tests, running them...

2) In what case, method About return diffrent ViewData? Check yourself. It's faster than unit tests

Let's see Account controller's test

Using this test we can check log in success. But it's more simple to run the application and type login/password manually

Please describe advantages of unit tests. In unit tests we'll enter data manually. So why can't we type it manually? Разъясните пожалуйста в чём их плюс. Если всё равно все тестировочные данные набиваешь руками не проще ли самому всё проверить

+2  A: 

You're right, these are Captain Obvious unit tests. In practice you'll write more complicated unit tests than this.

But it's more simple to run the application and type login/password manually

Once, maybe. But after every build? The point is automation: once you've built up a huge unit test suite you can use the test framework to execute it all automatically to catch regressions introduced with the last set of changes.

And then there's test-driven-development, etc. I expect there are good "Why unit test?" essays out there with much more.

Rup
+1  A: 

Fine, there are two reasons:

  1. Unit tests are automatic. All you need is to run your tests and check if everything is ok. In the case of manual testing you should manually check everything every time when you need to perform testing.

  2. Unit test called "unit" because you can test every method in isolation. So in case of error you can easily find the cause.

And read more about unit tests

DixonD