我是單元測試的新手 - 我只使用純粹的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));
}
}