Javascript 如何让 Jasmine 的 spyOnProperty 工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43928209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Jasmine's spyOnProperty to work?
提问by Angelo
I saw this postpost and was excited to try it out, but I'm unable to get it working. Trying to keep this simple just to figure out what's wrong, but even this is failing.
我看到了这个帖子,很高兴尝试一下,但我无法让它工作。试图保持这个简单只是为了找出问题所在,但即使这样也失败了。
export class SomeService {
...
private _myValue: Boolean = false;
get myValue(): Boolean {
return this._myValue;
}
set myValue(helper: Boolean) {
this._myValue = helper;
}
And in my unit test, I have:
在我的单元测试中,我有:
it('should ', inject([SomeService], (someService: SomeService) => {
let oldValue = someService.myValue;
expect(oldValue).toBe(false); // passes, clearly we can use our getter
someService.myValue = true;
expect(someService.myValue).toBe(true); // passed, clearly the setter worked
spyOnProperty(someService, 'myValue', 'getter').and.returnValue(false); // Property myValue does not have access type getter
//spyOnProperty(someService, 'myValue', 'get').and.returnValue(false);same error if tried this way
expect(someService.myValue).toBe(false);
}));
I put the values up top to clearly show I can get and set the value. That has no problems. Wallaby shows ReferenceError: spyOnProperty is not defined on the spyOnProperty line. I'm not sure if that helps, but the errors I put above were what karma gives me when I run those tests.
我将这些值放在最上面以清楚地表明我可以获取和设置该值。那没有问题。Wallaby 显示 ReferenceError: spyOnProperty 未在 spyOnProperty 行上定义。我不确定这是否有帮助,但我上面的错误是我运行这些测试时业力给我的。
Anyone who has gotten this to work, I'd greatly appreciate the assistance. Apologies for any typos, I've been staring at this for most of the day.
任何让这个工作的人,我将不胜感激。为任何错别字道歉,我一天中的大部分时间都在盯着这个。
回答by Angelo
Well I spent way more time on this then I care to admit, but the answer ended up being a simple syntactical error:
好吧,我花了更多的时间在这上面然后我愿意承认,但答案最终是一个简单的语法错误:
The correct value to use as the 3rd parameter is get, not getteras I had been.For example:
用作第三个参数的正确值是get,而不是getter像我以前那样。例如:
spyOnProperty(someService, 'myValue', 'get').and.returnValue(false)
Which I did try early on, but did not work at the time. I'm not sure what changed. I also updated @types/jasmine, along with everything else in my dev library to @latest, but I didn't restart the IDE afterward because I didn't think it'd matter. I can only guess that's why it works now.
我很早就尝试过,但当时没有用。我不确定发生了什么变化。我还将 @types/jasmine 以及我的开发库中的所有其他内容都更新为 @latest,但之后我没有重新启动 IDE,因为我认为这无关紧要。我只能猜测这就是它现在起作用的原因。
回答by Laoujin
I was still struggling a bit to get the setto work.
我仍然在努力让它set工作。
const foo = {
get value() {},
set value(v) {}
};
it('can spy on getters', () => {
spyOnProperty(foo, 'value', 'get').and.returnValue(1);
expect(foo.value).toBe(1);
});
it('and on setters', () => {
const spiez = spyOnProperty(foo, 'value', 'set');
foo.value = true;
expect(spiez).toHaveBeenCalled();
});

