简体   繁体   English

如何使用createSpy()使用Jasmine创建模拟对象

[英]How to creating mock objects with Jasmine using createSpy()

I'm trying to set up a mock unit test with jasmine by using createSpy(). 我正在尝试使用createSpy()设置使用jasmine的模拟单元测试。 I've getting 我得到了

TypeError: undefined is not a function (evaluating jasmine.createSpy().andCallFake(function(msg) { return [] }) ) (line 13). TypeError: undefined不是函数(评估jasmine.createSpy().andCallFake(function(msg) { return [] }) )(第13行)。

CODE: 码:

  $scope.workInit = function() {
    $scope.work_loading = true;
    $scope.public_work_loading = true;

    var projects = SomeService.getGroups();
    var publicProjects = SomeService.getPublicGroupings('G');
    …
     ...
  }

TEST: 测试:

 this.SomeService = {
    getGroups: jasmine.createSpy().andCallFake(function(msg) { return [] }),
    getPublicGroupings: jasmine.createSpy().andCallFake(function(msg) { return [] }),
}

  it('should expect work_loading and public_loading to be false', function () {
    this.scope.workInit();
    expect($scope.work_loading).toEqual(false);
    expect($scope.public_work_loading).toEqual(false);
  });

The way you have set up the mock unit test case is correct, the same works for me in this fiddle . 你设置模拟单元测试用例的方式是正确的,在这个小提琴中同样适用于我。

Check the scope in which you've included this part of the code: 检查您已包含此部分代码的范围:

this.SomeService = {
    getGroups: jasmine.createSpy().andCallFake(function(msg) { return [] }),
    getPublicGroupings: jasmine.createSpy().andCallFake(function(msg) { return [] }),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM