1

我是编程、OOP 和单元测试的新手,所以请原谅我缺乏知识。

作为我的 Rock, Paper and Scissors 游戏的一部分,我有一个抽象超类(Weapon ),它在 VB.NET中有子类(Rock, Paper 和 Scissors ),例如:

    Public MustInherit Class Weapons
         Public MustOverride Function compareTo(ByVal Weapons As Object) As Integer

    End Class

    Public Class Paper
        Inherits Weapons

        Public Overrides Function compareTo(ByVal Weapons As Object) As Integer
            If TypeOf Weapons Is Paper Then
                Return 0
            ElseIf TypeOf Weapons Is Rock Then
                Return 1
            Else
                Return -1
            End If
        End Function
    End Class

    Public Class Rock
        Inherits Weapons

        Public Overrides Function compareTo(ByVal Weapons As Object) As Integer
            If TypeOf Weapons Is Rock Then
                Return 0
            ElseIf TypeOf Weapons Is Scissors Then
                Return 1
            Else
                Return -1
            End If
        End Function
    End Class

    Public Class Scissors
        Inherits Weapons

        Public Overrides Function compareTo(ByVal Weapons As Object) As Integer
            If TypeOf Weapons Is Scissors Then
                Return 0
            ElseIf TypeOf Weapons Is Paper Then
                Return 1
            Else
                Return -1
            End If
        End Function
    End Class

还有一个超类Player,它具有子类(PlayerComputerRandomPlayerHumanPlayerPlayerComputerTactical),例如:

    Imports RockPaperScissors.Weapons

Public Class Player

    Private pName As String
    Private pNumberOfGamesWon As String
    Public pWeapon As Weapons

    Property Name() As String
        Get
            Return pName
        End Get
        Set(ByVal value As String)
            pName = value
        End Set
    End Property

    Property NumberOfGamesWon As String
        Get
            Return pNumberOfGamesWon
        End Get
        Set(ByVal value As String)
            pNumberOfGamesWon = value
        End Set
    End Property

    Property getWeapon As Weapons
        Get
            Return pWeapon
        End Get
        Set(ByVal value As Weapons)
            pWeapon = value
        End Set
    End Property

    Public Sub pickWeapon(ByVal WeaponType As String)
        If WeaponType = "Rock" Then
            pWeapon = New Rock()

        ElseIf WeaponType = "Paper" Then
            pWeapon = New Paper()

        Else
            pWeapon = New Scissors()

        End If

    End Sub

End Class



    Imports RockPaperScissors.Weapons

Public Class PlayerComputerRandom
    Inherits Player

    Private Enum weaponsList
        Rock
        Paper
        Scissors
    End Enum

    Public Overloads Sub pickWeapon()

        Dim randomChoice = New Random()
        Dim CompChoice As Integer = randomChoice.Next(0, [Enum].GetValues(GetType(weaponsList)).Length)

        If CompChoice = "0" Then
            pWeapon = New Rock()

        ElseIf CompChoice = "1" Then
            pWeapon = New Paper()

        Else
            pWeapon = New Scissors()

        End If


    End Sub

End Class



 Public Class PlayerComputerTactical
    Inherits Player

    Private plastMove As String

    Property lastMove() As String
        Get
            Return plastMove
        End Get
        Set(ByVal value As String)
            plastMove = value
        End Set
    End Property

    Public Overloads Sub pickWeapon()
        ' Add tactical player functionality
    End Sub


End Class


     Public Class PlayerHumanPlayer
        Inherits Player

    End Class

我有 GameForm 类,它实例化对象并执行用于前端的各种其他逻辑,如下所示:

    Public Class GameForm
    Private Sub btnRock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRock.Click
        findWinner("HumanPlayer", "Rock", "RandomComputer")
    End Sub

    Private Sub btnPaper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaper.Click
        findWinner("HumanPlayer", "Paper", "RandomComputer")
    End Sub


    Private Sub btnScissors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnScissors.Click
        findWinner("HumanPlayer", "Scissors", "RandomComputer")
    End Sub

    Public Sub findWinner(ByVal p1name As String, ByVal p1WeaponSelected As String, ByVal p2Name As String)
        Dim player1 = New PlayerHumanPlayer()
        Dim player2 = New PlayerComputerRandom()

        player1.Name = p1name
        player1.pickWeapon(p1WeaponSelected)  ' Should I be using the Rock Class???

        player2.Name = p2Name
        player2.pickWeapon()

        Dim winner As Integer = player1.getWeapon().compareTo(player2.getWeapon())

        Select Case winner
            Case 1
                txtGameStatus.Text = player1.Name() + " wins!"
            Case -1
                txtGameStatus.Text = player2.Name() + " wins!"
            Case 0
                txtGameStatus.Text = "Draw!"
        End Select
    End Sub

End Class

我需要使用 Visual Studio 2010 编写此单元测试。由于我从未这样做过,我不知道我可以包括哪些单元测试(除了立场添加/减去示例)。有人可以提供一些单元测试来开始。它会给我一个起点。

任何帮助将不胜感激。

提前谢谢

4

2 回答 2

2

我的第一个提示是将所有方法更改为返回值的函数,这是针对您要进行单元测试的方法。这样做将确保您可以使用该功能进行单元测试。

我可以为您提供结构方面的帮助,但恐怕我不会为您进行单元测试!

我将向您展示我在解决方案中的示例单元测试,它可能与您的解决方案无关,但结构仍然存在。首先我有我想测试的功能(添加),并且在一个单元测试项目中我有测试。

测试功能

Public Function Add(value1 As Int32, value2 As Int32)

    answer = value1 + value2

    Return answer

End Function

这是我在单元测试类中使用的单元测试

<TestMethod()> _
Public Sub AddTest()

    Dim target As Form1 = New Form1()
    Dim value1 As Int32 = 10
    Dim value2 As Int32 = 35
    Dim actual As Int32 = 45

    Dim result As Int32 = target.Add(value1, value2)

    Assert.AreEqual(result, actual)

End Sub
于 2012-12-20T14:03:00.707 回答
1

试着给你一个例子来说明你如何构建这个:

首先,而不是这个:

Public Overloads Sub pickWeapon()
   ' Logic...         
End Sub

你可能想做这样的事情:

 Public Overloads Function pickWeapon() as Weapons

    Dim selectedWeapon as Weapons = Nothing;

    // logic to set weapon here

    return selectedWeapon;
End Sub

现在您可以从 UnitTest 中调用它,并确保它返回一个有效的武器对象,例如(不一定是最有用的测试,但希望您明白这一点!:))。

如果您以类似的方式重组该方法findWinner(),您可以测试该方法的逻辑是否找到了正确的赢家。

但是请注意,您应该将寻找获胜者的逻辑与选择武器分开。也就是说,您应该在该方法之外创建播放器,并将它们传递给findWinner(). 这将允许您传递某些选项(武器),并检查您是否获得了预期的结果。目前你不能这样做,因为“player2”将使用的武器是由 random inside 选择的findWinner()

首先更改它,使其具有类似于以下的签名:

' Takes two players in, and returns the name of the winner
Public Sub findWinner(ByVal humanPlayer As PlayerHumanPlayer, 
                      ByVal computerPlayer As PlayerComputerRandom) As String

一个(非常简单的)测试,假设您已经重写了代码以适应这个:

<TestMethod()> _
Public Sub ShouldReturnPlayer1AsWinner()

    Dim player1 = New PlayerHumanPlayer("Human name", New Scissors())
    Dim player2 = New PlayerComputerRandom("Computer", New Rock())

    Dim result As String = findWinner(player1, player2)

    Assert.AreEqual(result, "Computer")

End Sub

同样,这可能不是最好的测试,但它应该给你一个想法,并帮助你开始。

旁注:我会称之为 super-class Weapon,而不是Weapons。可以这样想:你可以有很多不同的实例,它们可以是不同的武器(纸、剪刀等),但每个仍然是单一的武器。

于 2012-12-20T15:37:11.740 回答