单元测试中的计数方法调用 [英] Counting method invocations in Unit tests

查看:64
本文介绍了单元测试中的计数方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试中计算方法调用的最佳方法是什么.任何测试框架都允许吗?

What is the best way to count method invocations in a Unit Test. Do any of the testing frameworks allow that?

推荐答案

听起来您可能想使用模拟框架通常提供的.expects(1)类型方法.

It sounds like you may want to be using the .expects(1) type methods that mock frameworks usually provide.

使用模拟,如果您正在测试列表,并且想验证是否使用这些参数调用了clear 3次,并且add至少调用了一次,您可以执行以下操作:

Using mockito, if you were testing a List and wanted to verify that clear was called 3 times and add was called at least once with these parameter you do the following:

List mock = mock(List.class);        

someCodeThatInteractsWithMock();                 

verify(mock, times(3)).clear();
verify(mock, atLeastOnce()).add(anyObject());      

(来自 http://code.google.com/p/mockito/wiki/MockitoVSEasyMock )

这篇关于单元测试中的计数方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆