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

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 !";
    }
}

现在我想测试 Jasmine 的toHaveBeenCalled()匹配器。这是我写的
it("should be able to Login", function () {
    spyOn(authobj);
    expect(authobj.Login('abc', 'abc')).toHaveBeenCalled();
});

但它说undefined() method does not exist

最佳答案

编辑:看basecode answer以获得更好的方法

From the docs,您应该像下面这样使用它:

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();
});

10-07 19:03
查看更多