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
I've got the following code (I've simplified it):

Public Class MyClass

Public Function  Save() as integer
(returns a different value depending on whether Save was successful or not)
End Function 

Public Function Get() as String
(returns a string value depending on the current state of the application)
End Function 

Public Sub DoSomeMoreProcessing(a as integer, b as string)
(Does something different depending on the values of a and b)
End Function 

Public Sub Process()
      Dim a as integer = Save()
      Dim b as string = Get()
      DoSomeMoreProcessing(a, b)
End sub

End Class


Now Save, Get and DoSomeMoreProcessing are very expensive functions and all I want to do is just mock that they get called. However I want to check that DoSomeMoreProcessing is called with a and b. I cannot figure out how to make sure that DoSomeMoreProcessing is called with the correct arguments a and b.
asked by diangy (2.4k points)

5 Answers

0 votes
Hi
You can check for expected arguments like this:

Class TheClass
    Public Sub DoSomeMoreProcessing(ByVal a As Integer, ByVal b As String)
    End Sub
End Class

<TestFixture()> Public Class TestClass
    <Test()> Public Sub Test()
        Dim mock As Mock = MockManager.Mock(GetType(TheClass))
        mock.ExpectCall("DoSomeMoreProcessing").Args(5, "abc")

        Dim c As TheClass = New TheClass()
        c.DoSomeMoreProcessing(5, "abc")
    End Sub
End Class


More details on checking arguments you can find Here
answered by ohad (35.4k points)
0 votes
Hi
Sorry I didn't understand you the first time. :oops:
I guess what you need is the Dynamic return values feature
This will let you define your own delegate method to return different values.

Note that in order to use this feature you'll need an enterprise license.
Hope it helps.
answered by ohad (35.4k points)
0 votes
Actually I was able to get the following testing code to work which uses reflective typemocks:

     TypeMock.MockManager.Init()

            Dim accountDaoMock As TypeMock.Mock = TypeMock.MockManager.MockAll(GetType(AccountDao))

            'Account has to be saved
            accountDaoMock.ExpectAndReturn("SaveOrUpdate", Nothing).Args(anAccount)

            Dim emailTemplateDaoMock As TypeMock.Mock = TypeMock.MockManager.MockAll(GetType(EmailTemplateDao))

            'Email template has to be loaded from the database
            Dim anEmailTemplate As New EmailTemplate
            emailTemplateDaoMock.ExpectAndReturn("GetByApplicationNameAndName", anEmailTemplate).Args("Fax Rocket", "Account Activation")

            'Finally an activation email has to be sent
            Dim accountManagerMock As TypeMock.Mock = TypeMock.MockManager.MockAll(GetType(AccountManager))
            accountManagerMock.ExpectCall("SendActivationEmail").Args(anEmailTemplate, anAccount)

            anAccountManager.SignupAccount(anAccount, "Fax Rocket")

            TypeMock.MockManager.Verify()


I was trying to convert it to an equivalent using Natural Typemocks which is where I got stuck:

            Using recorder As New TypeMock.RecordExpectations
                recorder.DefaultBehavior.CheckArguments()

                'anAccountManager.AccountDao.SaveOrUpdate(anAccount)
                'recorder.Return(anAccount)

                Dim anEmailTemplate As New EmailTemplate
                recorder.ExpectAndReturn(anAccountManager.EmailTemplateDao.GetByApplicationNameAndName("Fax Rocket", "Account Activation"), anEmailTemplate)

                anAccountManager.SendActivationEmail(anEmailTemplate, anAccount)

            End Using
TypeMock.MockManager.Verify()


But I get an error in the last expected call to SendActivationEmail:

Call to Diangy.OnlineServiceFramework.Core.Accounts.AccountManager.SendActivationEmail() Parameter: 1
passed object [Diangy.OnlineServiceFramework.Core.Email.EmailTemplate] is not controlled by expected mock.
answered by diangy (2.4k points)
0 votes
By the way I have the Enterprise version already :)
answered by diangy (2.4k points)
0 votes
:idea: Here is the problem - When you call the following while recording:
Dim anEmailTemplate As New EmailTemplate

you are recording creating a new EmailTemplate.
So TypeMock is expecting a new instance of EmailTemplate to be created and passed to SendActivationEmail.

:arrow: To fix: move the creation line outside the recording block.
answered by scott (32k points)
...