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
Hi there.

Could someone explain how on earth do you translate this C# code to VB.NET:

public void FakeAnInstance_StubIncrementMethod()
{
    RealLogger fake = Isolate.Fake.Instance<RealLogger>();

    // Increment call will be ignored (stubbed)
    Isolate.WhenCalled(() => fake.Increment()).IgnoreCall();

    // Make sure Count is reset
    fake.Count = 0;

    // This call is faked - count should not increment
    fake.Increment();

    Assert.AreEqual(0, fake.Count);
}


I have node idea how to get this line to work:

Isolate.WhenCalled(() => fake.Increment()).IgnoreCall();


Could someone help me out with this?

Cheers.
Jas.
asked by MrClyfar (5.2k points)

5 Answers

0 votes
Hi,

Unfortunately, at this point you can't.

The reason is that VB doesn't support lambda expessions, and as for anonymous delegates, it doesn't support Action(Of T). So while you can use th AddressOf operator instead of the lambda, you can do this for expressions that return a value, not void ones. This is a language issue that will be resolved in VB 10.

But until the, you can use Natural or Reflective mocks. I can tell you that we are in the works of fitting the features of AAA to VB syntax. We'll keep you updated as we continue.

Sorry for the inconvenience,
answered by gilz (14.5k points)
0 votes
Hi there.

Cheers for the reply. After doing some research, before reading your post, I can see what you mean with the lack of support in VB.NET. No worries as I'm happy using C# anyway, thus I'll write my unit tests in C# (as seperate pojects), even if the main code was written in VB.

Cheers.
Jas.
answered by MrClyfar (5.2k points)
0 votes
How about
Isolate.WhenCalled(AddressOf fake.Increment).IgnoreCall


Will it work?
answered by ulu (1.7k points)
0 votes
Hi Artem,

It won't work if this is a Sub (void method). It compiles if it's a function (If I recall correctly).

We do have our VB API coming out soon. So there are going to be options.

Thanks,
answered by gilz (14.5k points)
0 votes
Hmm haven't tried that yet, but I see there are two overloads:

Public Shared Function WhenCalled(ByVal action As Action) As IVoidActionHandler
and
Public Shared Function WhenCalled(Of T)(ByVal func As Func(Of T)) As IPublicNonVoidMethodHandler(Of T)

The first one takes Action, so it should work with a delegate to a Sub. The other can take a Function lambda, I guess.

I'll be happy to use the VB version though.
answered by ulu (1.7k points)
...