0

我是单元测试的新手。单元测试在下面是成功的。我预计他们会失败,因为 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

我错过了什么?

4

1 回答 1

3

Your code isn't actually testing anything. It is simply setting up mocks. By themselves, mocks are useless. From what you've written, it appears you want to test the result of the getCourse1 function. An example of one of your tests might look like the following.

<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

The test method name follows a convention described in the book The Art of Unit Testing. The test body is arranged in a sequence known as the AAA pattern.

于 2013-07-07T17:59:48.490 回答