简体   繁体   English

如何让Jasmine的spyOnProperty工作?

[英]How to get Jasmine's spyOnProperty to work?

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);
 }));

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. Wallaby显示ReferenceError:未在spyOnProperty行上定义spyOnProperty。 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: 好吧,我花了更多的时间在这上面然后我愿意承认,但答案最终是一个简单的语法错误:

The correct value to use as the 3rd parameter is get , not getter as I had been. 用作第三个参数的正确值是get ,而不是像以前那样get getter For example: 例如:

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. 我还将@ types / jasmine以及我的开发库中的其他内容更新为@latest,但之后我没有重新启动IDE,因为我认为这不重要。 I can only guess that's why it works now. 我只能猜测这就是它现在有效的原因。

I was still struggling a bit to get the set to 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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM