如何让 Jasmine 的 spyOnProperty 工作?
我看到这篇文章 发布并很高兴尝试一下,但我无法让它工作.试图保持简单只是为了找出问题所在,但即使这样也失败了.
I saw this post post 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);
}));
我将这些值放在首位,以清楚地表明我可以获取和设置该值.那没有问题.Wallaby 显示 ReferenceError: spyOnProperty 未在 spyOnProperty 行上定义.我不确定这是否有帮助,但我上面提到的错误是我运行这些测试时业力给我的.
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.
如果有人得到这个工作,我将非常感谢您的帮助.抱歉任何错别字,我大部分时间都在盯着这个.
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.
推荐答案
好吧,我花了更多时间在这上面,然后我愿意承认,但答案最终是一个简单的语法错误:
Well I spent way more time on this then I care to admit, but the answer ended up being a simple syntactical error:
用作第三个参数的正确值是 get
,而不是像我以前那样的 getter
. 例如:
The correct value to use as the 3rd parameter is get
, not getter
as I had been. For example:
spyOnProperty(someService, 'myValue', 'get').and.returnValue(false)
我很早就尝试过,但当时没有奏效.我不确定发生了什么变化.我还将@types/jasmine 以及我的开发库中的所有其他内容更新为@latest,但之后我没有重新启动IDE,因为我认为这无关紧要.我只能猜测这就是它现在起作用的原因.
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.
相关文章