如何让Jasmine的spyOnProperty工作? [英] How to get Jasmine's spyOnProperty to work?

查看:274
本文介绍了如何让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.

这篇关于如何让Jasmine的spyOnProperty工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆