NUnit表示测试成功

时间:2013-07-07 14:40:24

标签: vb.net unit-testing mocking nunit nmock

我是Unit Testing的新手。单元测试在下面取得了成功。我希望它们失败,因为Student.getCourse,unidergraduate.getCourse1和postgraduate.getcourse1将不同的值返回到正在测试的内容,例如“学生课程”<> “学生课程1”。

Imports NUnit.Framework
Imports NMock
Imports StudentCL

Namespace Test

    <TestFixture()>
    Public Class StudentTest
        Private _mocks As MockFactory
        Private _StudentMock As Mock(Of Student)
        Private _GraduateMock As Mock(Of Graduate)
        Private _UndergraduateMock As Mock(Of UnderGraduate)

        <SetUp()>
        Public Sub SetUp()
            _mocks = New MockFactory()
            _StudentMock = _mocks.CreateMock(Of Student)()
            _GraduateMock = _mocks.CreateMock(Of Graduate)()
            _UndergraduateMock = _mocks.CreateMock(Of UnderGraduate)()
        End Sub

        <Test()>
        Public Sub getStudentCourse()
            _StudentMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Student Course1")
        End Sub

        <Test()>
        Public Sub getGraduateCourse()
            _GraduateMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Graduate Course1")
        End Sub

        <Test()>
        Public Sub getUndergraduateCourse()
            _UndergraduateMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Undergraduate Course1")
        End Sub

    End Class
End Namespace

Public MustInherit Class Student
    Public Overridable Function getCourse1() As String
        Return "Student course"
    End Function

End Class

Public Class Graduate
    Inherits Student

    Public Overrides Function getCourse1() As String
        Return "Graduate course"
    End Function

End Class

Public Class UnderGraduate
    Inherits Student

    Public Overrides Function getCourse1() As String
        Return "Undergraduate course"
    End Function

End Class

我错过了什么?

1 个答案:

答案 0 :(得分:3)

您的代码实际上并未测试任何内容。它只是设置模拟。嘲笑本身就没用了。根据您的编写,您似乎想要测试getCourse1函数的结果。您的某个测试的示例可能如下所示。

<Test()> _
Public Sub getCourse1_WhenCalled_ReturnsExpected()
    ' Arrange
    Dim student As New Graduate()

    ' Act
    Dim course As String = student.getCourse1()

    ' Assert
    Assert.That(course, Iz.EqualTo("Graduate Course1")) ' This will fail as you expected.
End Sub

测试方法名称遵循单元测试艺术一书中描述的约定。测试体按照称为AAA模式的顺序排列。