【问题标题】:Angular unit testing "If part not taken"角度单元测试“如果未采取部分”
【发布时间】:2019-11-27 14:17:47
【问题描述】:
 Dname = JSON.stringify(this.userDetails);
  Dpass = JSON.stringify({
    userName: 'test',
    password: 'test'
  });

  onSubmit() {
    this.userDetails = this.inputForm.value;
    this.submitted = true;
    if (this.Dname === this.Dpass) {
      this.router.navigate(['/page1']);
      console.log('ok');
    } else {
      console.log('not ok');
    }
  }

If path not taken --can someone help me to solve this issue in angular unit testing

【问题讨论】:

  • 不是图片,为什么不复制粘贴代码?
  • 图像很难处理 - 请粘贴代码
  • 我分享了代码截图
  • 嗨托尼,欢迎来到 stackoverflow。不要气馁,很多新的贡献者都会发布代码图片,但这是一个容易修复的错误。确保为所有未来的问题发布文本
  • 嗨 byxor ..对不起,我对此很陌生..感谢您的指导..我现在已经上传了我的代码 sn-p..提前谢谢大家

标签: angular unit-testing karma-jasmine


【解决方案1】:

您可以窥探this.router 的navigate 功能并使用toHaveBeenCalledWith 进行检查

【讨论】:

    【解决方案2】:

    您应该在 beforeEach testBed.configureTestingModule 中将它们设置为相等:

    .then(() => {
         fixture = TestBed.createComponent(YourComponent);
         component = fixture.componentInstance;
         component.Dname = component.Dpass
    });
    

    这些值将评估为 true,您可以通过使用 spyOn 来实现该方法并使用 .callThrough 访问该路径。这是一个例子:

    it('should execute methodName', () => {
            const spy = spyOn(component, 'methodName').and.callThrough();
            component.methodName();
            expect(spy).toHaveBeenCalled();
     });
    

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多