The following is my code and I am using the Test Project available in Visual Studio 2008
Public Class Calculator
Public Shared objCalculator As Calculator = Nothing
Private Sub New()
End Sub
Public Shared Function GetInstance() As Calculator
If objCalculator Is Nothing Then
objCalculator = New Calculator
End If
Return objCalculator
End Function
End Class
Test Method
'''<summary>
'''A test for GetInstance
'''</summary>
<TestMethod()> _
Public Sub GetInstanceTest()
Dim expected As Calculator = Nothing
Dim actual As Calculator
actual = Calculator.GetInstance
Assert.AreNotEqual(expected, actual)
Assert.Inconclusive("Verify the correctness of this test method.")
End Sub
Am I following the correct way to test the Singleton class. I am using Assert.AreNotEqual(expected, actual) this,
While you are using Assert.AreNotEqual()
correctly, the naming of expected
is a bit strange. A better name is probably notExpected
. Or just leave out the expected
variable and pass in Nothing
directly to Assert.AreNotEqual()
.
Also, singletons are dangerous things. Your test will, when calling GetInstance
, create a new Calculator
. The next test that calls GetInstance
will however not create a new Calculator
, but reuse the one that the first test created. What you have done is that you have introduced a dependency between the tests. That's something you typically don't want to have. My advise is that you don't use singletons.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.