试图了解Jasmine的toHaveBeenCalled()匹配器 [英] Trying to understand Jasmine's toHaveBeenCalled() matcher

查看:1166
本文介绍了试图了解Jasmine的toHaveBeenCalled()匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的 jasmine 这是我的 src 文件,我在其中创建 Auth class

I am new to jasmine here is my src file in which i create Auth class

function Auth() {
}

Auth.prototype.isEmpty = function(str) {
    return (!str || 0 === str.length);
}

Auth.prototype.Login = function (username , password) {
    if (this.isEmpty(username) || this.isEmpty(password)) {
        return "Username or Password cann't be blank ";
    }
    else {
        return "Logged In !";
    }
}

现在我想测试茉莉花的 toHaveBeenCalled() matcher。这是我写的

now i want to test jasmine's toHaveBeenCalled() matcher . Here is what i write

it("should be able to Login", function () {
    spyOn(authobj);
    expect(authobj.Login('abc', 'abc')).toHaveBeenCalled();
});

但它表示 undefined()方法不存在

推荐答案

编辑:看看基码回答以获得更好的方法

Look at basecode answer for a better approach

从文档中,您应该像下面这样使用它:

From the docs, you should use it like the following:

spyOn(foo, 'setBar');

it("tracks that the spy was called", function() {
  expect(foo.setBar).toHaveBeenCalled();
});

所以你应该写:

it("should be able to Login", function () {
  spyOn(authobj, 'isEmpty');  
  authobj.Login('abc', 'abc');  
  expect(authobj.isEmpty).toHaveBeenCalled();
});

这篇关于试图了解Jasmine的toHaveBeenCalled()匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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