如何调用一个模拟方法而不被间谍检测到? [英] How can a mocked method be called and not be detected by spy?

查看:70
本文介绍了如何调用一个模拟方法而不被间谍检测到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的组件:

@Component({
    selector: 'app-signup',
    templateUrl: './signup.component.html',
    styleUrls: ['./signup.component.scss']
})
export class SignUpComponent implements OnInit {
    specialLink: string;

    constructor(
        private activatedRoute: ActivatedRoute,
    ) {
        this.specialLink = this.activatedRoute.snapshot.params.id;
        console.log('TEST1', this.specialLink);

        if (this.specialLink !== undefined) {
            this.setSpecialSignup();
        }
    }

    setSpecialSignup() {
        console.log("CALLED;");
    }

这是我的测试:

describe('SignUpComponent', () => {
  let component: SignUpComponent;
  let fixture: ComponentFixture<SignUpComponent>;
  let ActivatedRouteMock: any;
  
  beforeEach(async(() => {
    ActivatedRouteMock = {
      snapshot: {
        params: { id: 123 }
      },
    };

    TestBed.configureTestingModule({
      declarations: [ SignUpComponent ],
      imports: [ RouterTestingModule ],
      providers: [
        {provide: ActivatedRoute, useValue: ActivatedRouteMock}
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(SignUpComponent);
    component = fixture.componentInstance;
  });


  describe('Patient Side', () => {
    it('should call setSpecialSignup() when user is coming from specialLink', () => {
      spyOn(component, 'setSpecialSignup');
      fixture.detectChanges();
      expect(component.setSpecialSignup).toHaveBeenCalled();
    });

我正在测试是否已调用setSpecialSignup.就是这样! 2'console.log'证明了这一点.然后,为什么会出现此错误:Expected spy setSpecialSignup to have been called.? 我想念什么?

I'm testing if setSpecialSignup has been called. And it is ! The 2 'console.log' prove it. Then, why do I get this error : Expected spy setSpecialSignup to have been called.? What did I miss ?

看来我需要能够更改ActivatedRouteMock.snapshot.params.id的值.有时它必须是未定义的,有时它必须是某物".我怎样才能做到这一点 ?我尝试了ActivatedRouteMock.snapshot.params.id = 'something',但是即使使用fixture.detectChanges(),它也不会改变任何值.我该怎么办?

It looks like I need to be able to change the value of ActivatedRouteMock.snapshot.params.id. Sometimes it has to be undefined and sometimes it has to be 'something'. How can I do that ? I tried ActivatedRouteMock.snapshot.params.id = 'something' but it doesn't change the value at all, even with fixture.detectChanges(). How can I do that ?

推荐答案

setSpecialSignupconstructor内部被调用.在此行fixture = TestBed.createComponent(SignUpComponent);上调用constructor.而且您创建间谍太迟了.如果希望此功能易于测试,请将组件的逻辑移至ngOnInit生命周期挂钩.它会在第一个fixture.detectChanges()上调用,您的测试应该没问题

as we see setSpecialSignup is called inside of a constructor. constructor is called on this line fixture = TestBed.createComponent(SignUpComponent);. and you create a spy too late. if you want this feature to be testable easier, move logic of your component to ngOnInit lifecycle hook. it will be called on the first fixture.detectChanges() and your test should be fine

constructor(
        private activatedRoute: ActivatedRoute,
    ) { }
   ngOnInit() {
        this.specialLink = this.activatedRoute.snapshot.params.id;
        console.log('TEST1', this.specialLink);

        if (this.specialLink !== undefined) {
            this.setSpecialSignup();
        }
    }

这篇关于如何调用一个模拟方法而不被间谍检测到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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