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

时间:2016-06-07 15:21:26

标签: javascript unit-testing mocking jasmine

我正在尝试使用createSpy()使用jasmine设置模拟单元测试。我得到了

  

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

1 个答案:

答案 0 :(得分:0)

您设置模拟单元测试用例的方式是正确的,this fiddle对我来说同样适用。

检查您已包含此部分代码的范围:

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