chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hello,

I can't get TypeMock to work properly. Maybe someone can help?

This is the code I would to mock. Unfortunately it always uses the real implementation. I'm sure I activated typeMock. In the trace I see that the mocked object is defined but never called (as far as I understand the information)


Thanks in advance for your help.

Best regards,

Alexander


....... VS2005 test framework ......
   <TestMethod()> _
      Public Sub HelloTest()
        MockManager.Init()
        Dim target As Greetings = New Greetings
        Dim helloMock As Mock = MockManager.Mock(GetType(GreetingsLibrary))
        helloMock.Strict = True
        helloMock.ExpectConstructor()
        helloMock.ExpectAndReturn("Hello", "Ann", GetType(System.String))
        Dim expected As String = "Ann"
        Dim actual As String
        actual = target.getGreeting
        Assert.AreEqual(expected, actual, "Greetings.getGreeting did not return the expected value.")
        MockManager.Verify()
    End Sub
End Class


Public Class Greetings
    Private glib As GreetingsLibrary
    Public Sub New()
        glib = New GreetingsLibrary
    End Sub
    Public Function getGreeting() As String
        Return glib.Hello
    End Function
End Class

Public Class GreetingsLibrary
    Public Sub New()
        'intentionally
    End Sub
    Public Function Hello() As String
        Return "world"
    End Function
End Class
asked by anowak (640 points)

1 Answer

0 votes
Hello,

Never mind. I found my error . I instantiated my object to early ... :oops:

TestMethod()> _ 
      Public Sub HelloTest() 
        MockManager.Init() 
       ' Dim target As Greetings = New Greetings ' too early
        Dim helloMock As Mock = MockManager.Mock(GetType(GreetingsLibrary)) 
        helloMock.Strict = True 
        helloMock.ExpectConstructor() 
        helloMock.ExpectAndReturn("Hello", "Ann", GetType(System.String)) 
        Dim target As Greetings = New Greetings 
        Dim expected As String = "Ann" 
        Dim actual As String 
        actual = target.getGreeting 
        Assert.AreEqual(expected, actual, "Greetings.getGreeting did not return the expected value.") 
        MockManager.Verify() 
    End Sub 
End Class
answered by anowak (640 points)
...