FakeItEasy VB.NET 参数问题
好的,我正在尝试自学使用模拟框架进行测试,我在 VB.NET 中工作,我对 lambda 表达式很陌生,我以前的所有应用程序都是在 2005 或更早版本中编写的。我现在已经2010年了。 所以我尝试过Rhino.Mocks,但发现很难理解它,主要是因为旧的语法。由于这些天似乎没有人在 VB.NET 中写博客,所以我一直在查看 C# 示例并试图弄清楚发生了什么。
因此,我遇到一种情况,我将一个接口传递给类的构造函数并保存对该接口的引用。当在对象上调用方法并引发事件时,应由实现接口的类处理。
我遇到了麻烦,所以我尝试用 C# 创建一个简单的版本,并在 vb.net 中重复这些步骤。
所以我的界面:
public interface IBroadcastClient
{
void MessageReceivedHandler(string msg);
}
引发事件的类:
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);
}
}
}
测试:
[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();
}
通过,到目前为止没有问题。
现在在 vb.net 中尝试同样的操作; 相同的接口和广播类,只是在 vb.net 中而不是 C# 中,最初进行以下单元测试。
<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
此操作失败并显示以下错误消息; " 以下调用断言失败: TestFakeItEasyVB.IBroadcastClient.MessageReceivedHandler(消息:) 预计至少会找到一次,但在调用中发现了 #0 次: 1: TestFakeItEasyVB.IBroadcastClient.MessageReceivedHandler(msg: "Yay!")"
这样写很有趣;
<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
也会失败并出现相同的错误消息,但是,这个版本的测试通过了。
<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
这个变体在 C# 中也通过了,我的困惑我做错了什么让测试忽略传递给伪造事件处理程序的参数?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NextCall 语法的存在是出于遗留原因,最好使用表达式语法:
在您的测试中,高于所有其他的测试都具有 MustHaveHappened,但这个特定的测试具有 MustNotHaveHappened,我想这就是您的测试失败的原因。我已经编译了您的代码并运行它,一旦将其更改为 MustHaveHappened,测试就会通过。
目前,您不能在 VB 特定的“NextCall”语法中使用参数约束。但是,您可以使用方法“WhenArgumentsMatch”来重写您的第一个测试,如下所示:
或者您可以使用扩展“WithAnyArguments”来忽略所有参数:
The NextCall-syntax is there for legacy reasons, it's better to use the expression syntax:
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:
Or you could use the extension "WithAnyArguments" to ignore all arguments: