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 have been trying to mock a shared method and Isolator has been throwing an ArgumentException when it executes.

My method returns only a boolean value:
Public Shared Function IsUserInRole(userid as long, role as Role) as Boolean

Here is my test method:
    <Test> _
    <Isolated> _
    Public Sub Authenticate_IsValidUser_Success()
        Dim objUser As UserEntity = FakeInstance(Of UserEntity)()
        objUser.IsActive = True
        objUser.IsRegistered = True

        Dim mock As MockObject = MockManager.MockObject(GetType(IAuthenticationService))
        mock.ExpectAndReturn("ValidateLogin", objUser)
        Dim auth As IAuthenticationService = mock.Object

        FakeSharedMethods(Of RoleService)(Members.MustSpecifyReturnValues)
        Using TheseCalls.WillReturn(True)
            RoleService.IsUserInRole(0, Role.Student)
        End Using

        Dim user As UserEntity = New AuthenticationService(auth).Authenticate("testuser", "testuser1", Role.Student)
        MockManager.Verify()
    End Sub

I have been scratching my head trying to figure out this one, and I've determined that the source is the multiple parameter static function. If I change my IsUserInRole function to take only a single parameter, the test will pass.

The funny thing is, when I run the test with the 2 parameter method in debugger mode, I try to step through and find the error, but the test passes!

Any ideas on what could be causing the issue?

here is the error I am getting:
TestCase 'Tests.AuthenticationServiceTests.Authenticate__ValidUser_Success'
failed: System.ArgumentException : Method 'Void Authenticate__ValidUser_Success()' is not defined for type 'System.Object'
at System.Linq.Expressions.Expression.ValidateCallInstanceType(Type instanceType, MethodInfo method)
at System.Linq.Expressions.Expression.ValidateCallArgs(Expression instance, MethodInfo method, ReadOnlyCollection`1& arguments)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at e4.c()
at e4.a()
at e4.b()
asked by underwhelmed (1.7k points)

5 Answers

0 votes
I need to further investigate this issue but from a quick glance I see that you have a mix of both old API (reflective mocks) and AAA Visual Basic API. This is not recommended because it infects the default behavior of the fake/mock.

I would suggest to stick with one API and see if the problem still occurs.
answered by dhelper (11.9k points)
0 votes
If the ValidateLogin is public, I would reqrite the test like this:
  <Test> _ 
    <Isolated> _ 
    Public Sub Authenticate_IsValidUser_Success() 
        Dim objUser As UserEntity = FakeInstance(Of UserEntity)() 
        objUser.IsActive = True 
        objUser.IsRegistered = True 

   dim svc = FakeInstance(Of IAuthenticationService)()
   Using TheseCalls.WillReturn(objUser)      
      svc.ValidateLogin()
   End Using  

        FakeSharedMethods(Of RoleService)(Members.MustSpecifyReturnValues) 
        Using TheseCalls.WillReturn(True) 
            RoleService.IsUserInRole(0, Role.Student) 
        End Using 

        Dim user As UserEntity = New AuthenticationService(auth).Authenticate("testuser", "testuser1", Role.Student) 
    End Sub


notice that I don't use Verify here, because I'm not sure what it is that you are trying to test.

If the ValidateLogin is not public, the current work around is to add a reference to the C# API and use this API in your test for the non public:


    <Test> _ 
    <Isolated> _ 
    Public Sub Authenticate_IsValidUser_Success() 
        Dim objUser As UserEntity = FakeInstance(Of UserEntity)() 
        objUser.IsActive = True 
        objUser.IsRegistered = True 

   dim svc = FakeInstance(Of IAuthenticationService)()

   'this is using the C# API as a current work around
   Isolate.NonPublic.WhenCalled(svc,"ValidateLogin").WillReturn(objUser)

        FakeSharedMethods(Of RoleService)(Members.MustSpecifyReturnValues) 
        Using TheseCalls.WillReturn(True) 
            RoleService.IsUserInRole(0, Role.Student) 
        End Using 

        Dim user As UserEntity = New AuthenticationService(auth).Authenticate("testuser", "testuser1", Role.Student) 
    End Sub
answered by royo (2k points)
0 votes
I changed around my syntax to use VB AAA to this:
<Test> _
<Isolated> _
Public Sub Authenticate__ValidUser_Success()
   Dim objUser As UserEntity = FakeInstance(Of UserEntity)()
   objUser.IsActive = True
   objUser.IsRegistered = True

   Dim auth As IAuthenticationService = FakeInstance(Of IAuthenticationService)(Members.ReturnRecursiveFakes)
   Using TheseCalls.WillReturn(objUser)
      auth.ValidateLogin(String.Empty, String.Empty)
   End Using

   FakeSharedMethods(Of RoleService)(Members.MustSpecifyReturnValues)
   Using TheseCalls.WillReturn(True)
      RoleService.IsUserInRole(0, Role.Student)
   End Using

   Dim user As UserEntity = New AuthenticationService(auth).Authenticate("testuser", "testuser1", Role.Student)
   Assert.IsTrue(objUser.IsRegistered AndAlso objUser.IsActive)
End Sub


I now get this error:
TestCase 'MyProject.Business.Tests.AuthenticationServiceTests.Authenticate__ValidUser_Success'
failed: TypeMock.TypeMockException :
*** No method calls found inside using block
at e4.c()
at e4.a()
at e4.b()
D:WebsitesMyProjectUnitTestsMyProject.Business.TestsServicesSecurityAuthenticationServiceTests.vb(19,0): at MyProject.Business.Tests.AuthenticationServiceTests.Authenticate__ValidUser_Success()
at Typemock.Isolator.VisualBasic.IsolatedAttribute.Execute()
at Typemock.Isolator.VisualBasic.IsolatedAttribute.Execute()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
D:WebsitesMyProjectUnitTestsMyProject.Business.TestsServicesSecurityAuthenticationServiceTests.vb(7,0): at MyProject.Business.Tests.AuthenticationServiceTests.Authenticate__ValidUser_Success()


Stepping through the debugger, the test returns this output at the end of the first Using statement. I don't understand what this error message means. The first line of the Authenticate method is to call the IAuthenticateService.ValidateLogin function.
answered by underwhelmed (1.7k points)
0 votes
I need to further investigate this issue - lets take it offline
answered by dhelper (11.9k points)
0 votes
If anyone has this problem, it has to do with TestDriven.NET and running the tests on a 64-bit machine.

http://blog.typemock.com/2009/01/soluti ... hines.html

Thanks,
Steve
answered by underwhelmed (1.7k points)
...