Я следую примерам в Professional.Test.Driven.Development.with.Csharp. Я конвертирую код из С# в 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
Просто для ссылки на тот же код в С#. Тест не прошел
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);
В экземпляре объекта не задана ссылка на объект.
Существуют тонкие различия в том, как переменные типа ценности ведут себя в VB.NET по сравнению с С#.
В С# нет значения, назначенного автоматически при создании этой переменной; это значение null
(Nothing
). Поэтому вы не можете использовать их непосредственно после объявления; первая операция над этой переменной должна быть назначением.
С другой стороны, в VB.NET значение по умолчанию присваивается при первом использовании. 0
для чисел, False
для Boolean
, 1/1/0001 12:00 AM
для Date
, пустая строка для String
и т.д. Они не равны нулю. Поэтому вы можете начать использовать их сразу после объявления.
например
С#:
int i;
i++; //you can't do this
int j = 0;
j++; // this works
VB.NET:
Dim i As Integer
i += 1 ' works ok
Dim j As Integer = 0
j += 1 ' this also works
РЕДАКТИРОВАТЬ:
Прочитайте комментарии о поведении переменных типа String
.
String
является ссылочным типом , а не типом значения .
_testItemType
в_testItemType
c #