ASP.NET MVC - 单元测试矫枉过正? (TDD) [英] ASP.NET MVC - Unit testing overkill? (TDD)

查看:75
本文介绍了ASP.NET MVC - 单元测试矫枉过正? (TDD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我开始赶TDD错误,但我想知道如果我真的这样做是正确的......我好像在写测试的一个LOT

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.

例如,这些都是从我的AccountController登录操作:

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自我解释。

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,

查尔斯

TIA,
Charles

推荐答案

这一切都取决于你有多少报道需要/想和多少可靠性是一个问题。

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

这里有问题,你应该问自己:


  • 这是否单元测试有助于实现功能/ code修改,我还没有?

  • 请问这个单元测试帮助回归测试/调试这个单位,如果我做出改变以后呢?

  • 是code,以满足这个单元测试不平凡的或值得为它写单元测试?

  • 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?

关于第三届之一,我记得当我开始写单元测试(我知道,不是一回事TDD)我会测试,会是这样:

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.

我推荐这篇文章ASP.net MVC书作者桑德森:

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

<一个href=\"http://blog.$c$cville.net/2009/08/24/writing-great-unit-tests-best-and-worst-practises/\">http://blog.$c$cville.net/2009/08/24/writing-great-unit-tests-best-and-worst-practises/

这篇关于ASP.NET MVC - 单元测试矫枉过正? (TDD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆