我正在关注Professional.Test.Driven.Development.with.Csharp中的示例, 我正在将代码从 C# 转换为 VB。(这个例子是第 7 章的开始)
现在有
Public Class ItemType
Public Overridable Property Id As Integer
End Class
Public Interface IItemTypeRepository
Function Save(ItemType As ItemType) As Integer
End Interface
Public Class ItemTypeRepository
Implements IItemTypeRepository
Public Function Save(ItemType As ItemType) As Integer Implements IItemTypeRepository.Save
Throw New NotImplementedException
End Function
End Class
并在 TestUnit 项目中
<TestFixture>
Public Class Specification
Inherits SpecBase
End Class
在 vb.net 中编写测试时(应该失败)它很好地通过了(整数值 0 = 0)
Public Class when_working_with_the_item_type_repository
Inherits Specification
End Class
Public Class and_saving_a_valid_item_type
Inherits when_working_with_the_item_type_repository
Private _result As Integer
Private _itemTypeRepository As IItemTypeRepository
Private _testItemType As ItemType
Private _itemTypeId As Integer
Protected Overrides Sub Because_of()
_result = _itemTypeRepository.Save(_testItemType)
End Sub
<Test>
Public Sub then_a_valid_item_type_id_should_be_returned()
_result.ShouldEqual(_itemTypeId)
End Sub
End Clas
仅供参考 C# 中的相同代码。测试失败
using NBehave.Spec.NUnit;
using NUnit.Framework;
using OSIM.Core;
namespace CSharpOSIM.UnitTests.OSIM.Core
{
public class when_working_with_the_item_type_repository : Specification
{
}
public class and_saving_a_valid_item_type : when_working_with_the_item_type_repository
{
private int _result;
private IItemTypeRepository _itemTypeRepository;
private ItemType _testItemType;
private int _itemTypeId;
protected override void Because_of()
{
_result = _itemTypeRepository.Save(_testItemType);
}
[Test]
public void then_a_valid_item_type_id_should_be_returned()
{
_result.ShouldEqual(_itemTypeId);
}
}
}
测试在此行失败:
_result = _itemTypeRepository.Save(_testItemType);
你调用的对象是空的。