【问题标题】:Error in spyOn toHaveBeenCalled in model testing模型测试中的 spyOn toHaveBeenCalled 错误
【发布时间】:2014-12-03 15:00:27
【问题描述】:

我正在尝试测试 Jasmine 的 toHaveBeenCalled() 匹配器。 我的来源:

function myModel (){
}

  myModel.prototype.getObjcts = function (){
    return DS.findAll('obj', {});
  }

还有茉莉花测试:

 describe('findAll', function(){
      it('check calling findAll()', function(){
        spyOn(DS, 'findAll');
        myModel.getObjcts();
        expect(DS.findAll('obj', {})).toHaveBeenCalled();
      });
    });

但我总是遇到错误

预期是间谍,但未定义

请帮我看看哪里出了问题

【问题讨论】:

    标签: angularjs unit-testing jasmine


    【解决方案1】:
    1. 您应该实例化一个 myModel 类型的新对象
    2. 您的期望再次调用 DS.finadAll 而不是检查间谍。

    这里是完整的解决方案:

    window.DS = {
        findAll: function (param1, param2) {}
    };
    
    
    function myModel() {}
    
    myModel.prototype.getObjcts = function () {
        return DS.findAll('obj', {});
    };
    
    
    describe('findAll', function () {
        it('check calling findAll()', function () {
            spyOn(DS, 'findAll');
            var testApp = new myModel();
            testApp.getObjcts();
            expect(DS.findAll).toHaveBeenCalledWith('obj', {});
        });
    });
    

    你可以在 jsfiddle here找到一个可运行的解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2017-12-24
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多