doudengjin8251 2015-12-10 14:36
浏览 104

如何在Codeception \ Util \ Stub中正确使用atLeastOnce方法?

I'm using codeception for testing my PHP app, and there is a method I don't know how to use. It's called Stub::atLeastOnce() and, like Codeception's documentation of the Stub class says:

"Checks if a method has been invoked at least one time. If the number of invocations is 0 it will throw an exception in verify."

But when I try to use it, doesn't matter that I comment the call to User::getName() or not, the test passes.

My user class looks like this:

<?php

class User {
  public function getName() {
    return 'pepito';
  }

  public function someMethod() {

  }
}  

And my test method like this:

public function testStubUsage() {
    // all methods that the stub impersonates must be, at least, defined
    $user = Stub::make('User', array('getName' => Stub::atLeastOnce(function() { return 'Davert'; }), 'someMethod' => Stub::atLeastOnce('User::getName')));
    $user->getName();
}

So, what is the usage of that function to make the test fail if User::getname() is never called?

  • 写回答

1条回答 默认 最新

  • doumi1311 2015-12-10 15:39
    关注

    The doc is not correct, you have to pass $this as third argument of Stub::make() for this to work properly:

    public function testStubUsage() {
        $user = Stub::make('User', array('getName' => Stub::atLeastOnce(function() { return 'Davert';}), 'someMethod' => function() {}), $this); // <- Here
        $user->getName();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题