Best use of fake method in Visual Basic

4 posts, 0 answers
  1. Michel Corbin
    Michel Corbin avatar
    20 posts
    Member since:
    Apr 2010

    Posted 22 Jul 2010 Link to this post

    Hi,

    I try to use the mocking framework with visual basic and I try to figure it out what is the best manner to fake a method.

    When I try to fake a function, the syntaxe is very nice like this:

          <Test()> _
          Public Sub TestDelete()
             Dim fileManager As JustMockFile.IFileManager

             fileManager = Mock.Create(Of JustMockFile.IFileManager)()

             Mock.Arrange(Function() fileManager.Delete(Arg.AnyString)).Returns(False)

             Assert.IsTrue(fileManager.Delete(""))
          End Sub

    As you can see, the keyword "Function()" is use and when I call the "Delete" function of the FileManager, I have a full support with intellisense to define possibles values of each arguments.

    The problem is when I try to fake a method because I don't know how to call Arrange correctly except with the keyword "AddressOf". The problem with "AddressOf" is that I don't have a full support with intellisense and i'm limited with 9 parameters.

          <Test()> _
          Public Sub TestCreate()
             Dim fileManager As JustMockFile.IFileManager

             fileManager = Mock.Create(Of JustMockFile.IFileManager)()

             Mock.Arrange(AddressOf fileManager.Create, Arg.AnyString)

             Mock.Assert(AddressOf fileManager.Create, Arg.AnyString, Occurs.Once)
          End Sub

    I have full of legacy applications where many methods have more then 9 parameters. Any ways, I don't have full support with Intellisense.

    I want to be able to fake a method like this:

          <Test()> _
          Public Sub TestCreate()
             Dim fileManager As JustMockFile.IFileManager

             fileManager = Mock.Create(Of JustMockFile.IFileManager)()

             Mock.Arrange(function() fileManager.Create(Arg.AnyString))

             Mock.Assert(function() fileManager.Create(Arg.AnyString) Occurs.Once)
          End Sub

    Is there any way to fake method like a function with Visual Basic... with the "Function()" keyword?

    Thank you very much
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 23 Jul 2010 Link to this post

    Hi Samuel,

    Thanks a lot for posting such topic. The action overload with max 9 parameters is for VB 3.5 only where the support for lambda using sub is not available. In that case, we provide a separate visual basic assembly. But for VB 4 you don’t need it anymore and you can directly use Telerik.JustMock.dll as VB 4 subs fully support lambda. Just to note that you will only get these legacy options, if you are referencing Telerik.JustMock.VisualBasic.dll.
     
    In short, for functions you can  use the Function() keyword but for subs (void methods) you need to call it with address of.

    But we will ponder over it whether  to support legacy syntax like record-reply to tackle VB sub issue.

    Hope that helps,
    Mehfuz


    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. Michel Corbin
    Michel Corbin avatar
    20 posts
    Member since:
    Apr 2010

    Posted 23 Jul 2010 Link to this post

    Thank you,

    I think it time for me to upgrade to Visual Studio 2010 with Visual Basic or simply use C#. :)
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 23 Jul 2010 Link to this post

    Hello Samuel,

    Yeah upgrading to visual studio 2010 and VB 4 is the best solution,  no more tricky workaround for VB 3.5 Sub issues :-). The best part is that you can use C# API as it is in visual basic.

    Hope that helps,
    Mehfuz


    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top