【问题标题】:New to TDD in asp.NET, am I on the right track writing tests?asp.NET 中的 TDD 新手,我是否在正确的轨道上编写测试?
【发布时间】:2011-03-27 10:27:02
【问题描述】:

在过去的几个月里,我阅读了很多关于 TDD 的内容,并决定通过一个简单的示例来尝试一下,但我只是不确定我是否在实践中测试了正确的东西。这里是用于验证电子邮件的自定义数据注释的测试:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MembershipTest.Tests
{
    [TestClass]
    public class CustomDataAnnotationsTest
    {
        [TestMethod]
        public void CustomDataAnnotations_Email_ReturnTrueIfNull()
        {
            // Arrange
            EmailAttribute attribute = new EmailAttribute();

            // Act
            bool result = attribute.IsValid(null);

            // Assert
            Assert.AreEqual(true, result);
        }

        [TestMethod]
        public void CustomDataAnnotations_Email_ReturnFalseIfInvalid()
        {
            // Arrange
            EmailAttribute attribute = new EmailAttribute();

            // Act
            bool result = attribute.IsValid("()[]\\;:,<>@example.com");

            // Assert
            Assert.AreEqual(false, result);
        }

        [TestMethod]
        public void CustomDataAnnotations_Email_ReturnTrueIfValid()
        {
            // Arrange
            EmailAttribute attribute = new EmailAttribute();

            // Act
            bool result = attribute.IsValid("john.smith@example.com");

            // Assert
            Assert.AreEqual(true, result);
        }
    }
}

这是为测试编写的后续代码:

using System;
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;

public class EmailAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        //Let RequiredAttribute validate whether the value is required or not.
        if (value == null)
        {
            return true;
        }

        //Check to see if System.Net.Mail can send to the address.
        try
        {
            var i = new MailAddress(value.ToString());
        }
        catch (Exception)
        {
            return false;
        }

        return true;
    }

}

所有测试最初都失败,然后在编写代码后成功,但是测试是否正确编写?太多了,还是太少了?我知道这是一个非常简单的示例,但我想确保自己走在正确的轨道上,然后再进行更复杂的事情。

【问题讨论】:

  • 也许完全跑题了,但我还是要说:转储 VS 集成测试的东西,取而代之的是 NUnit。与 MSBuild 与 NAnt 一样,VSTS 并没有开始提供您在 NUnit 中发现的广泛的功能和灵活性。除此之外,我认为您的处理方式是正确的。
  • 这确实是完全题外话 :) 我认为这是个人喜好问题。我使用 Visual Studio 测试工具,它们对我来说效果很好,都集成在 VS 中。
  • @Coco:它们也很好地集成到了 TFS 中。
  • 我知道;我并不是要暗示其他 :) 我以前使用过 NUnit 和 TestDriven.NET,我确实喜欢它。但是你必须为商业用途付费,而且我发现开箱即用的 VS 东西对我来说很好。但这确实是题外话,我不想进一步模糊原始问题 - 在这里已经详细讨论过:stackoverflow.com/questions/92869/…
  • 如果我是你,我会从你的代码中抛弃“Arrange”、“Act”和“Assert” cmets。只需用换行符分隔每件事。此外,我会用测试名称获得更多描述性。 “ReturnTrueIfValid”和“ReturnFalseIfInvalid”很明显,并不能说明测试的实际作用。我也不同意您验证电子邮件的方式,但那是另一回事了。

标签: asp.net asp.net-mvc unit-testing testing tdd


【解决方案1】:

我认为你是在正确的轨道上。在这一点上,我建议在您的测试中进行一些重构。由于您正在使用

EmailAttribute attribute = new EmailAttribute();

在每个测试中。我建议创建 TestInitialize() 和 TestCleanup() 方法。 TestInitialize 将新建 EmailAttribute,而 TestCleanup 将使对象清空。这只是一个偏好问题。像这样

private EmailAttribute _attribute;

[TestInitialize]
public void TestInitialize()
{
  _attribute = new EmailAttribute
}

[TestCleanup]
public void TestCleanup()
{
  _attribute = null;
}

【讨论】:

  • 很好,使用 SetUp/TearDown 绝对是一个好习惯 :)
  • 你的观点很有道理,但是使用Visual Studio单元测试工具,我认为这些属性应该分别是TestInitializeTestCleanup(你使用的是NUnit):D
  • 谢谢可可瓦拉。我已经更改了属性名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多