GlutVonSmark
GlutVonSmark

Reputation: 322

why test passes in vb? It should fail (it fails in the c# version)

I'm following examples in Professional.Test.Driven.Development.with.Csharp I'm converting the code from C# into VB. (this example is the start of chapter 7)

Now having

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

And in the TestUnit project

<TestFixture>
Public Class Specification
   Inherits SpecBase
End Class

When writing the test in vb.net (should fail) it passes nicely (integer value of 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

Just for reference same code in C#. Test fails

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);
        }
    }
}

Test fails on this line:

_result = _itemTypeRepository.Save(_testItemType);

Object reference not set to an instance of an object.

Upvotes: 1

Views: 156

Answers (1)

Pradeep Kumar
Pradeep Kumar

Reputation: 6969

There are subtle differences how value-type variables behave in VB.NET vs. C#.

In C# there is no value assigned automatically when that variable is created; it is null (Nothing). So you can't start using them directly after declaration; the first operation on that variable must be an assignment.

On the other hand, in VB.NET a default value is assigned on its first use. 0 for numbers, False for Boolean, 1/1/0001 12:00 AM for Date, empty string for String etc. They are not null. So you can start using them immediately after declaration.

e.g.

C#:

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

EDIT:

Please read comments about behavior of String type variables.

Upvotes: 2

Related Questions