【问题标题】:Jasmine setter method mock is not working茉莉二传手方法模拟不起作用
【发布时间】:2020-12-14 04:17:56
【问题描述】:

component.ts

fillformdata(): void {
    if (this.mark === false) {
      var jsoncontent = {
        ipaddr: (this.noip == false) ? this.ipinput : this.ipadd,
        host: (this.noip == false) ? this.hostinput : this.host,
        username: this.usernamevalue,
        password: this.passwordvalue
      };
      this.deployt.setuserinputs(jsoncontent);
    } else {

    }
  }

在上述方法中,我尝试将表单数据设置为setuserinputs,这是 n depoyClass 文件

component.spec.ts

it('should call fillformdata', () => {
    var jsoncontent = {
      ipadd:"127.0.0.1",
      host:"host1",
      username:"admin",
      password:"admin"
    };
    let response = component.fillformdata(); 
    const deploy =  new deploymentdetails();
    const spiez = spyOnProperty(deploy, 'setuserinputs', 'set');
    deployinput.userinputs = [jsoncontent];
    expect(spiez).toHaveBeenCalled();
    expect(response).not.toBeNull();
  });

当我执行ng test 失败时。显示以下错误

Error: <spyOnProperty> : Property setuserinputs does not have access type set
        Usage: spyOnProperty(<object>, <propName>, [accessType])
            at <Jasmine>

我关注了下面的堆栈链接(第二个答案) How do I spyOn Typescript getters and setters?

【问题讨论】:

  • 能否在此处提供您的 ts 和 spec.ts 文件,方便查找解决方案。
  • this.deployt 是什么?请提供代码。

标签: angular typescript jasmine angular-test


【解决方案1】:

看起来你想监视组件的部署变量。

it('should call fillformdata', () => {
    var jsoncontent = { ... };
    let response = component.fillformdata(); 

    // Spy on component.deployt
    const spiez = spyOn(component.deployt, 'setuserinputs');

    deployinput.userinputs = [jsoncontent];
    expect(spiez).toHaveBeenCalled();
    expect(response).not.toBeNull();
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 2012-02-13
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多