如何在 phpunit 中对不返回值的函数进行单元测试?
这就是我想测试的课程。具体来说,我只是选择我想要测试的功能之一。而 var 是从 classB 中执行某些函数返回的值,bar 是来自 classC 的实例,然后执行一些传递一些变量的函数。对于大多数提示/示例,要测试的函数是返回一个值。所以我的问题是,如何测试这个特定功能是否有效?
谢谢。
class mA extends A {
...
function doSomething($foo) {
$var = doStuffFromClassB("hallo");
$bar = ClassC::instance();
$bar->doStuffFromClassC($var, $foo, "world");
}
}
so this is the class that i want to test. and specifically i just pick one of the function that i want to test. while var is a value returned from doing some function from classB bar is instance from classC and then do some function which pass some variables. for most of the hints/example, the function to be tested is return a value. so my question is, how to test that this particular function worked?
thanks.
class mA extends A {
...
function doSomething($foo) {
$var = doStuffFromClassB("hallo");
$bar = ClassC::instance();
$bar->doStuffFromClassC($var, $foo, "world");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它被称为
doSomething
并且它没有通过返回值来指示它的作用,那么您可以使用 mock对象来跟踪与其他对象的交互。请参阅 PhpUnit 的有关模拟对象的文档。我想在这种情况下,您想要验证
doStuffFromClassC
方法是否与doStuffFromClassB
中的 var 相关。If it's called
doSomething
and it doesn't indicate what it does by returning a value, then you can use mock objects to trace the interaction with the other objects.See PhpUnit's documentation on mock objects. I guess in this case you want to verify that the
doStuffFromClassC
method is involved with the var fromdoStuffFromClassB
.