Sinon STUB在测试文件中有效,但在实际的代码文件中无效

时间:2019-04-27 08:22:56

标签: sinon

我创建了sinon STUB,该存根使用存根(“ password-hash”:“ ^ 1.2.2”)PasswordHash的'generate'方法。当我在TEST文件中运行PasswordHash.generate('mnop')方法时,这里会发生什么。但是,当在实际代码中调用PasswordHash.generate方法时,该方法将不起作用,并且将调用原始的PasswordHash.generate方法,而不是调用存根控件。

import * as PasswordHash from 'password-hash'

class UserDetails {

    constructor(email: String, password: String, roles: String[], departments: String[], hashPassword: Boolean) {
        this.email = email
        /*  In this console.log it will call the original PasswordHash.generate method 
         *  instead of called stubbed one (means it will print big hashed password)
         */
        console.log(PasswordHash.generate('mnop')
        this.password = hashPassword ? PasswordHash.generate(password.toString()) : password
        this.roles = roles
        this.departments = departments
    }
    validatePassword(password: String): Boolean {
        // return PasswordHash.verify(password.toString(), this.password.toString());
        return password.toString() === this.password.toString();
    }

}
export default UserDetails
$table->bigIncrements('id');

0 个答案:

没有答案