起订量 +单元测试——如何将一个操作传递给我的类来测试该操作是否被调用?
基本上,我的类中有一个方法,如果满足某些条件,它会调用 Action
。如何进行单元测试以确保调用该操作?
public class MyClass<T>
{
private IDBService _dbService;
private Action<T> _action;
public MyClass(IDBService dbService, Action<T> action)
{
if (dbService == null) throw new ArgumentNullException("dbService");
if (action == null) throw new ArgumentNullException("action");
_dbService = dbService;
_action = action;
}
public void CallActionIfPossible(T param)
{
if (_dbService.IsTopUser)
action(param);
}
}
Basically, I have a method on my class that invokes an Action<T>
if certain conditions are met. How can I unit test to ensure that the action is invoked?
public class MyClass<T>
{
private IDBService _dbService;
private Action<T> _action;
public MyClass(IDBService dbService, Action<T> action)
{
if (dbService == null) throw new ArgumentNullException("dbService");
if (action == null) throw new ArgumentNullException("action");
_dbService = dbService;
_action = action;
}
public void CallActionIfPossible(T param)
{
if (_dbService.IsTopUser)
action(param);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,基本思想是
Action
在某处产生一些状态变化(如果没有,那有什么意义?)。因此,单元测试当条件成立时预期的状态更改会发生,而当条件不成立时预期的状态更改不会发生。当然,理想情况下,您可以模拟
Action
,以便状态测试非常简单。您不需要 Moq 或任何其他模拟框架:当然
,我不知道您的确切设置。也许您在构建
Bar
时传入action
,或者您可能有其他设置操作的方法。但这个想法应该很清楚。Well, the basic idea is that the
Action<T>
produces some state change somewhere (if it doesn't, what's the point?). So, unit test that the expected state change occurs when the conditions hold, and that the expected state change doesn't occur when the conditions do not hold.Of course, ideally, you mock the
Action<T>
so that the state testing is super easy. You do not need Moq or any other mocking framework for this:and
Of course, I don't know your exact setup. Maybe you pass in
action
on construction ofBar
, or maybe you have some other way of setting the action. But the idea should be clear.Jason 的回答很好,但经常被忽视的一个警告是,您经常需要测试一定数量的调用(例如,不仅被调用,而且只被调用一次)。所以我经常做这样的事情:
Jason's answer is good, but one caveat that is often overlooked is that you will often need to test for a certain number of invocations (e.g. not only was it called, but it was called exactly once). So I often do something like this: