NullReferenceException vb.net 单元测试错误

发布于 2024-12-04 21:26:11 字数 1061 浏览 0 评论 0原文

做了一些关于单元测试的作业,老实说,我只是不明白为什么会出现这个错误。

我有一个简单的方法,可以在选项卡时选择所有文本并测试是否实际选择了 textbox1。我只是不明白为什么我在 target.txtbox1_Enter(sender, e) 处收到 nullReferenceException

我真的很生气,因为这看起来很容易。

任何帮助我将不胜感激。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2024-12-11 21:26:11

这将是正确的做法。

'''<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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文