Javascript 错误:<;已装入>;:预期是间谍,但得到了功能

Javascript 错误:<;已装入>;:预期是间谍,但得到了功能,javascript,jasmine,Javascript,Jasmine,这是测试代码 var list = new List([1, 2, 3, 4]); var list2 = new List([5, 1]); beforeAll(function () { spyOn(list.values, 'map').and.callThrough(); list.map(plusOne); }); it('Array.prototype.map()', function () { expect(list.values.map).not.toHaveB

这是测试代码

var list = new List([1, 2, 3, 4]);
var list2 = new List([5, 1]);

beforeAll(function () {
  spyOn(list.values, 'map').and.callThrough();

  list.map(plusOne);
});

it('Array.prototype.map()', function () {
  expect(list.values.map).not.toHaveBeenCalled();
});

This results in the following error 1) List must not call native Array function Array.prototype.map()   Message:
    Error: <toHaveBeenCalled> : Expected a spy, but got Function.
    Usage: expect(<spyObj>).toHaveBeenCalled()

  class List {
    constructor(clist = []) {
        this.values = clist;
    }
    map(f) {
        var temp = [];
        this.values.forEach((item, index) => {
                   temp.push(f(item));
                });
        this.values = temp;
        return this;
    }
}
module.exports = { List };
var list=新列表([1,2,3,4]);
var list2=新列表([5,1]);
beforeAll(函数(){
spyOn(list.values,'map')。和.callThrough();
列表.地图(plusOne);
});
它('Array.prototype.map()',函数(){
expect(list.values.map).not.tohaveBeenCall();
});
这将导致以下错误1)列表不能调用本机数组函数Array.prototype.map()消息:
错误::应为间谍,但获得了函数。
用法:expect().toHaveBeenCalled()
班级名单{
构造函数(clist=[]){
this.values=clist;
}
地图(f){
var-temp=[];
this.values.forEach((项目,索引)=>{
温度推送(f(项目));
});
该值=温度;
归还这个;
}
}
module.exports={List};
我不认为这是单元测试失败,因为无论调用not.tohavebeencall()还是tohavebeencall(),我都会收到相同的消息

我正在使用节点8.9.4和jasmine 2.8.0

我相信语法是正确的,因为当我针对这些测试运行其他代码时,它们通过了测试。但是我的代码导致了这个错误

我的问题是,上面的错误是什么意思?
关于,

我刚刚运行了以下测试,它在jasmine@3.0.0

fit('Spy on map works', () => {
        let someArray = [1, 3, 5];
        spyOn(someArray, 'map').and.callThrough();
        someArray.map(function(r){ console.log(r); });
        expect(someArray.map).toHaveBeenCalled();
 });
您可能希望运行此示例以了解它在测试中是否有效

正如我在评论中所说,您的list map方法使用新数组覆盖list.values。因此,间谍不再存在了。尝试以下方法:

someArray.forEach((item, index) => {
                   someArray[index] = f(item);
});
要解释正在发生的事情:

//WORKS
 fit('Spy on map works', () => {
    let someArray = [1, 3, 5];
    spyOn(someArray, 'map').and.callThrough();
    someArray.forEach((item, index) => {
        someArray[index] = (item + '_t');
    });
    someArray.map(function(r){ console.log(r); });
    expect(someArray.map).toHaveBeenCalled();
});
//FAILS because array is another object.
fit('Spy on map fails', () => {
    let someArray = [1, 3, 5];
    spyOn(someArray, 'map').and.callThrough();

    let tempArray = [];
    someArray.forEach((item, index) => {
        tempArray.push(item + '_t');
    });
    someArray = tempArray;

    someArray.map(function(r){ console.log(r); });
    expect(someArray.map).toHaveBeenCalled();
});
然而,您可以只监视原型。诸如此类:

 spyOn(Array.prototype, 'map').and.callThrough();

然后,您的测试就可以了。

谢谢您的帮助。我升级了茉莉花。同样的结果。正如我所提到的,我确实有来自其他地方的代码。因此,我的代码显然存在一个需要修复的问题。我不明白的是,这个错误实际上意味着什么。我的类列表有一个映射方法。包含值的类变量(数组)。测试是检查类是否使用List.map而不是array.map。事实上,我可以删除not并得到相同的错误,这表明测试没有失败,但是有一个错误。List类中的map函数是否正在重新实例化values数组?是的,List map正在用一个新的对象数组覆盖这些值。因此,你的间谍不适用于这个新对象。这很奇怪。当我运行您的测试时,它工作正常。我更改了代码,以便复制、清空值,然后对其执行操作,我得到了与“expected a spy…”相同的结果。我认为我做了一些根本错误的事情,我明白你的意思。但是如果我运行另一个版本的代码,它就可以工作了。如果我运行我的代码版本,则会出现错误。如果我从另一个代码中获取代码并将该方法粘贴到我的代码中,它将失败。正常的单元测试可以工作。失败的只是间谍。你想让我把代码放在GitHub的摘要中吗?