我是单元测试的新手 - 我只使用纯粹的Testmethods(我的最后一个模块,我创建了其中的约50个)完成了基本的断言测试。了解一些单元测试的做法
我正在读单元测试一本书,书中的众多例子中的一个已经创造了我对每个单独测试一个新的类。以下是为一个测试用例创建的示例对象之一。我的问题是有没有必要这样做?或者什么时候应用这种方法,何时不需要?
public class and_saving_an_invalid_item_type : when_working_with_the_item_type_repository
{
private Exception _result;
protected override void Establish_context()
{
base.Establish_context();
_session.Setup(s => s.Save(null)).Throws(new ArgumentNullException());
}
protected override void Because_of()
{
try
{
_itemTypeRepository.Save(null);
}
catch (Exception exception)
{
_result = exception;
}
}
[Test]
public void then_an_argument_null_exception_should_be_raised()
{
_result.ShouldBeInstanceOfType(typeof(ArgumentNullException));
}
}