0

Doing some homework on unit testing and in all honestly i just don't get why i get this error.

i got a simple method that select all text when tab and testing if the actually textbox1 is selected. I just don't get why i'm getting a nullReferenceException at target.txtbox1_Enter(sender, e)

I'm getting really mad because this seem so easy.

any help i would appreciate it.

Private Sub txtbox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtbox1.enter
    Dim txtBox As TextBox
    txtBox = CType(sender, TextBox)
    txtBox.SelectAll()
End Sub

    <TestMethod(), DeploymentItem("frmtesting.exe")>
Public Sub txtCustomer_EnterTest()
    Dim target As frmtesting_Accessor = New frmtesting_Accessor ' TODO: Initialize to an appropriate value
    Dim sender As Object = Nothing ' TODO: Initialize to an appropriate value
    Dim e As EventArgs = Nothing ' TODO: Initialize to an appropriate value

    Dim expected As String
    Dim actual As String
    target.txtbox1.Text = "bob"
    target.txtbox1.SelectAll()
    expected = "bob"

    target.txtbox1_Enter(sender, e)

    actual = target.txtbox1.SelectedText
    Assert.AreEqual(expected, actual, "not equal")
End Sub
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
Tom
  • 149
  • 2
  • 4
  • 11
  • 1
    `Sender` is null. `Dim sender as Object = Nothing` and then you pass it in to `txtbox1_Enter`. – Randy Levy Sep 18 '11 at 01:06
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Sep 18 '11 at 01:09

1 Answers1

0

This would be the correct way of doing it.

'''<summary>
    '''A test for TextBox1_Enter
    '''</summary>
    <TestMethod(), _
     DeploymentItem("frmtesting.exe")> _
    Public Sub TextBox1_EnterTest()
        Dim target As frmtesting.exe_Accessor = New frmtesting.exe_Accessor()
        Dim sender As Object = target.TextBox1
        Dim e As EventArgs = Nothing

        target.txtBox1.Text = "bob"

        target.txtBox1_Enter(sender, e)

        Assert.AreEqual("bob", target.txtBox1.SelectedText, "not equal")

    End Sub
chrissie1
  • 5,014
  • 3
  • 26
  • 26