你如何在 Jasmine 中测试 Typescript 函数?
How do you test a Typescript function in Jasmine?
这是一个直截了当的问题,但我找不到答案。 Jasmine 文档可以改进。它主要展示了监视模拟数据和函数的示例,但我想测试我编写的函数。否则,我实际上在测试什么?当我使用 Pipes 时,我发现它更直接,因为我所要做的就是用 new 关键字实例化管道。然后断言它 returns 我所期望的。
例如
const test = pipe.transform('8726324632');
expect(test).toEqual('872-632-4632');`
但是,功能似乎不起作用。取下面的代码:
export function getKeyValueOfAttributesFromElement(el) {
const attributeMap = {};
Object.keys(el.attributes)
.forEach(key => {
attributeMap[el.attributes[key].nodeName] = el.attributes[key].value;
});
return attributeMap;
};
//我的测试
import { getKeyValueOfAttributesFromElement } from './dom';
describe('getKeyValueOfAttributesFromElement', () => {
let mockData;
beforeEach(() => {
mockData = {
attributes: {
a: 'somestring',
b: 42,
c: false
}
};
});
it('should work', () => {
const myFunc = getKeyValueOfAttributesFromElement(mockData);
expect(myFunc).toHaveBeenCalled();
});
});
我在浏览器中收到以下错误,而不是这个传递 expected a Spy, but got ({ undefined: undefined }).)
如果我用 new
关键字调用函数,我得到
Only a void function can be called with the 'new' keyword.
我在这里错过了什么?我怎样才能在我的测试中调用我的函数而不用编一个?
嗯,getKeyValueOfAttributesFromElement
不是间谍,Jasmine 是这么说的。 toHaveBeenCalled
方法只能应用于 Jasmine 间谍对象。 myFunc
是处决的结果,不是间谍
你必须首先存根你的 getKeyValueOfAttributesFromElement
函数,但事实证明创建一个独立导出函数的间谍 (see github discussion) 是非常棘手的。
我建议根本不要检查 toHaveBeenCalled
,因为它没有任何意义。您通常在依赖项上使用 toHaveBeenCalled
来单独测试您的函数,而不是在您刚刚调用的原始函数上。只需针对不同的数据集测试结果,在这种情况下,这将是 100% 正确的方法。
Jasmine 错误可能具有欺骗性。显然问题出在 .toHaveBeenCalled()
方法上。
当我更新模拟并使用 .toEqual()
和 .not.toBeNull()
时,它通过了。
describe('getKeyValueOfAttributesFromElement', () => {
let mockData;
it('should return null', () => {
mockData = {
attributes: {
a: {
nodename: 'something',
value: 'a value'
}
}
};
expect(getKeyValueOfAttributesFromElement(mockData)).not.toBeNull();
});
it('should return attributes', () => {
mockData = {
attributes: {
a: {
nodename: 'something',
value: 'a value'
}
}
};
expect(getKeyValueOfAttributesFromElement(mockData)).toEqual({'undefined': 'a value'});
});
it('should return invalid output', () => {
mockData = {
attributes: {
a: {
nodename: 'something',
value: 'a value'
},
b: {
name: false,
value: ''
}
}
};
expect(getKeyValueOfAttributesFromElement(mockData)).toEqual({'undefined': ''});
});
});
这是一个直截了当的问题,但我找不到答案。 Jasmine 文档可以改进。它主要展示了监视模拟数据和函数的示例,但我想测试我编写的函数。否则,我实际上在测试什么?当我使用 Pipes 时,我发现它更直接,因为我所要做的就是用 new 关键字实例化管道。然后断言它 returns 我所期望的。 例如
const test = pipe.transform('8726324632');
expect(test).toEqual('872-632-4632');`
但是,功能似乎不起作用。取下面的代码:
export function getKeyValueOfAttributesFromElement(el) {
const attributeMap = {};
Object.keys(el.attributes)
.forEach(key => {
attributeMap[el.attributes[key].nodeName] = el.attributes[key].value;
});
return attributeMap;
};
//我的测试
import { getKeyValueOfAttributesFromElement } from './dom';
describe('getKeyValueOfAttributesFromElement', () => {
let mockData;
beforeEach(() => {
mockData = {
attributes: {
a: 'somestring',
b: 42,
c: false
}
};
});
it('should work', () => {
const myFunc = getKeyValueOfAttributesFromElement(mockData);
expect(myFunc).toHaveBeenCalled();
});
});
我在浏览器中收到以下错误,而不是这个传递 expected a Spy, but got ({ undefined: undefined }).)
如果我用 new
关键字调用函数,我得到
Only a void function can be called with the 'new' keyword.
我在这里错过了什么?我怎样才能在我的测试中调用我的函数而不用编一个?
嗯,getKeyValueOfAttributesFromElement
不是间谍,Jasmine 是这么说的。 toHaveBeenCalled
方法只能应用于 Jasmine 间谍对象。 myFunc
是处决的结果,不是间谍
你必须首先存根你的 getKeyValueOfAttributesFromElement
函数,但事实证明创建一个独立导出函数的间谍 (see github discussion) 是非常棘手的。
我建议根本不要检查 toHaveBeenCalled
,因为它没有任何意义。您通常在依赖项上使用 toHaveBeenCalled
来单独测试您的函数,而不是在您刚刚调用的原始函数上。只需针对不同的数据集测试结果,在这种情况下,这将是 100% 正确的方法。
Jasmine 错误可能具有欺骗性。显然问题出在 .toHaveBeenCalled()
方法上。
当我更新模拟并使用 .toEqual()
和 .not.toBeNull()
时,它通过了。
describe('getKeyValueOfAttributesFromElement', () => {
let mockData;
it('should return null', () => {
mockData = {
attributes: {
a: {
nodename: 'something',
value: 'a value'
}
}
};
expect(getKeyValueOfAttributesFromElement(mockData)).not.toBeNull();
});
it('should return attributes', () => {
mockData = {
attributes: {
a: {
nodename: 'something',
value: 'a value'
}
}
};
expect(getKeyValueOfAttributesFromElement(mockData)).toEqual({'undefined': 'a value'});
});
it('should return invalid output', () => {
mockData = {
attributes: {
a: {
nodename: 'something',
value: 'a value'
},
b: {
name: false,
value: ''
}
}
};
expect(getKeyValueOfAttributesFromElement(mockData)).toEqual({'undefined': ''});
});
});