开发者

FakeItEasy VB.NET issues with parameters

Ok, I am trying to teach myself testing using a mock framework and I work in VB.NET, I am new to lambda expressions and all my previous applications were written in version 2005 or earlier. I now have 2010. So I have tried Rhino.Mocks but found it difficult to get my head around it mostly because of the older syntax. Since, no-one seems to be bloggin in VB.NET these days, I have been looking at C# examples and trying to figure out what is going on.

So I have a situation where I pass an interface to the constructor of a class and hold a refrence to that interface. When an method is called on the object and event is raise that should be handled by the class that implements the inteface.

I was having trouble, so I tried to create a simple version in C# and repeat the steps in vb.net.

So my interface:

public interface IBroadcastClient
{
   void MessageReceivedHandler(string msg);
}

The class that raises the events:

public class Broadcaster
{     
    public Broadcaster(IBroadcastClient c)
    {
        _client= c;
        this.SendMessage += new MessageReceived(_client.MessageReceivedHandler);
    }
    private IBroadcastClient _client;


    public event MessageReceived SendMessage;

    public void SendMessageNow()
    {
        string _Message;
        if (SendMessage != null)
        {
            _Message = @"Yay!";
            SendMessage(_Message);
        }
    }
}

The test:

[TestMethod]
public void TestSendMessageWithIgnoreParameter()
{
    //string msg = @"Yay!";
    var client = A.Fake<IBroadcastClient>();
    Broadcaster b = new Broadcaster(client);
    b.SendMessageNow开发者_运维知识库();
    A.CallTo(() => client.MessageReceivedHandler(A<string>.Ignored)).MustHaveHappened();
}

This passes, no problems so far.

Now to try the same this in vb.net; The same interface and broadcaster class, just in vb.net rather than C# with initially hte following unit test.

<TestMethod()>
Public Sub TestMethod1()
    Dim client = A.Fake(Of IBroadcastClient)()
    Dim b As New Broadcaster(client)
    b.SendMessageNow()
    NextCall.To(client).MustHaveHappened()
    client.MessageReceivedHandler(A(Of String).Ignored)

End Sub

This fails with the following error message; " Assertion failed for the following call: TestFakeItEasyVB.IBroadcastClient.MessageReceivedHandler(msg: ) Expected to find it at least once but found it #0 times among the calls: 1: TestFakeItEasyVB.IBroadcastClient.MessageReceivedHandler(msg: "Yay!")"

Funnily enough writing it this way;

<TestMethod()>
Public Sub TestMethod3()
    Dim client = A.Fake(Of IBroadcastClient)()
    Dim b As New Broadcaster(client)
    b.SendMessageNow()
    A.CallTo(Sub() client.MessageReceivedHandler(A(Of String).Ignored)).MustNotHaveHappened()


End Sub

Will also fail with the same error message, however, this version of the test passes.

<TestMethod()>
Public Sub TestMethod2()
    Dim client = A.Fake(Of IBroadcastClient)()
    Dim b As New Broadcaster(client)
    b.SendMessageNow()
    NextCall.To(client).MustHaveHappened()
    client.MessageReceivedHandler("Yay!")

End Sub

This variation also passes in C#, my quandry is what am I doing wrong to get the test to ignore the argument passed to the faked event handler?


The NextCall-syntax is there for legacy reasons, it's better to use the expression syntax:

A.CallTo(Sub() client.MessageReceivedHandler(A(Of String).Ignored)).MustNotHaveHappened()

In your tests above all others has MustHaveHappened, but this specific one has MustNotHaveHappened, I guess that's why your test is failing. I've compiled your code and run it and once it's changed to MustHaveHappened the test passes.

Currently you can not use argument constraints in the VB-specific "NextCall"-syntax. However you can use the method "WhenArgumentsMatch" to rewrite your first test like this:

<TestMethod()>
Public Sub TestMethod1()
    Dim client = A.Fake(Of IBroadcastClient)()
    Dim b As New Broadcaster(client)
    b.SendMessageNow()
    NextCall.To(client).WhenArgumentsMatch(Function(a) a.Get(Of String)(0) = "Yay!").MustHaveHappened()

    client.MessageReceivedHandler(Nothing)
End Sub

Or you could use the extension "WithAnyArguments" to ignore all arguments:

<TestMethod()>
Public Sub TestMethod1()
    Dim client = A.Fake(Of IBroadcastClient)()
    Dim b As New Broadcaster(client)
    b.SendMessageNow()
    NextCall.To(client).WithAnyArguments().MustHaveHappened()

    client.MessageReceivedHandler(Nothing)
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜