VB.NET developers have long searched for an easy, effective tool for unit testing their code. Typemock Isolator has come to the rescue by making it easy for VB.NET developers to write unit tests in their native language with custom Visual Basic APIs.
The problem with unit testing VB.NET Available unit testing tools are not built to support VB.Net out of the box and have often treated the programming language as a second-class citizen. Frequently, the tools make you adapt to them, rather than the other way around. The main problem in unit testing, which is to isolate the tested code from its dependencies, still remains in VB. Therefore, writing unit tests in VB.NET has remained a big challenge. Typemock’s Isolator has come along to solve this problem and make VB.NET unit testing simple.
Typemock Isolator’s solution Typemock Isolator comes with a set of Visual Basic APIs. The APIs are simple and elegant, have the familiar Arrange-Act-Assert structure and were devised with VB in mind. As a VB.NET developer, you’ll feel right at home with regular VB constructs, like Using blocks – until now, virtually unheard of when unit testing VB.NET.
The VB APIs expose the entire feature set of Isolator’s powerful mocking engine. You can now mock any dependency – Frameworks like SharePoint, ASP.Net, WCF or any 3rd party libraries. You can mock Shared and NotInheitable classes, or any other code. With Typemock Isolator, VB.NET developers can now write clear, concise and effective unit tests in their language of expertise.
Isolator’s VB.NET unit test in action The clearest way to see Isolator’s powers is with an example. Let’s say I have a data collection form. I want to test the closing event – if the user closes the window, and the property IsDirty is true, it should show a confirmation box; conversely, it will close if IsDirty is false. Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
Public Class MyForm Private Sub MyForm_FormClosing (ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing If Me.IsDirty = True Then Dim result As DialogResult result = MessageBox.Show("Do you really want to close the window?", "Confirm Closing", MessageBoxButtons.OKCancel) If result = DialogResult.Cancel Then e.Cancel = True Else e.Cancel = False End If End If End Sub Public ReadOnly Property IsDirty() As Boolean Get Throw New NotImplementedException End Get End Property End Class |
The problem is that IsDirty throws an exception. If I try to invoke the event directly, my test will fail because of this exception. In addition, even if I manage to solve this problem when calling MessageBox.Show the test will hang, waiting for a manual operation. This is decidedly detrimental to an automated test.
Now let’s write a test with Isolator’s API to change the behavior of the IsDirty as well as the MessageBox.Show calls. Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
<TestMethod()> Public Sub WhenClosing_IsDirtyIsTrue_UserChoseToCancel_DontCloseForm() Dim form As New MyForm() ' Override the IsDirty property and return True Using TheseCalls.WillReturn(True) Dim temp = form.IsDirty End Using ' Override the MessageBox.Show call to return OK, ' simulating a "Don't Close" Using TheseCalls.WillReturn(DialogResult.Cancel) MessageBox.Show("","",MessageBoxButtons.OK) End Using ' Invoke the closing event using the Close method form.Show() form.Close() ' Assert the form did not close Assert.IsTrue(form.Visible) End Sub |
As you can see, we use Isolator’s TheseCalls.WillReturn API to specify the return values of the calls. We then invoke the closing event. Because of our setup, the original code is ignored and instead returns what we specified. An isolated, easy VB.NET unit test is born.
Download a free trial of Typemock’s VB.NET Unit Testing tool to start writing your own VB.Net tests or click through our resources below for all the information and support you need to effectively unit test in VB.NET.
Download Typemock Isolator VB.NET Unit Testing Tool!
VB.NET Unit Testing Resources:
|