TypeError: .subscribe is not a function Jasmine Angular

huangapple go评论94阅读模式
英文:

TypeError: .subscribe is not a function Jasmine Angular

问题

以下是您要翻译的内容:

  1. I am trying to write unit test for my angular component.
  2. I am able to mock 2 methods successfully using spyOn.
  3. But one method (_paymentStateService.getMakePaymentState$) is giving an error even after I mock and use callFake.
  4. It is giving error (**this._payStateService.getMakePaymentState$.subscribe is not a function**) in these line:
  5. spyOn(mockMakePaymentState, 'getMakePaymentState$')
  6. fdescribe('DetailsComponent', () => {
  7. let component: DetailsComponent;
  8. let fixture: ComponentFixture<DetailsComponent>;
  9. let httpMock: HttpTestingController;
  10. let formModule: FormsModule;
  11. let form = new FormGroup({
  12. username: new FormControl('', Validators.required),
  13. dob: new FormControl('')
  14. });
  15. let mockbillingService = {
  16. getDetails: () => {},
  17. getBankDetails: () => {}
  18. }
  19. let mockMakePaymentState = {
  20. getPaymentDetails$: () => of({} as IPaymentDetails),
  21. getDetails: () => {},
  22. getUserInfo: () => {}
  23. };
  24. class MatDialogMock {
  25. // When the component calls this.dialog.open(...) we'll return an object
  26. // with an afterClosed method that allows to subscribe to the dialog result observable.
  27. open() {
  28. return {
  29. afterClosed: () => of({action: true})
  30. };
  31. }
  32. }
  33. let paymentInfoDetails = <PaymentInfoDetails>{};
  34. let paymentBankDetails = <PaymentBankDetails>{};
  35. let makePaymentState = <IPaymentDetails> {}
  36. paymentInfoDetails.banks = [paymentBankDetails];
  37. beforeEach(async () => {
  38. await TestBed.configureTestingModule({
  39. declarations: [ ],
  40. imports: [ FormsModule, ReactiveFormsModule],
  41. providers: [
  42. { provide: BillingService, useValue: mockbillingService },
  43. {provide: PayState, useValue: mockMakePaymentState},
  44. { provide: MatDialog, useClass: MatDialogMock }
  45. ]
  46. })
  47. .compileComponents();
  48. fixture = TestBed.createComponent(DetailsComponent);
  49. component = fixture.componentInstance;
  50. httpMock = TestBed.inject(HttpTestingController);
  51. formModule = TestBed.inject(FormsModule);
  52. // fixture.detectChanges();
  53. });
  54. it('should call ngOnInit', () => {
  55. spyOn(mockMakePaymentState, 'getDetails').and.callFake(() => {
  56. return of(
  57. paymentInfoDetails
  58. );
  59. });
  60. spyOn(mockMakePaymentState, 'getBankPayments').and.callFake(() => {
  61. return of(
  62. paymentBankDetails
  63. );
  64. });
  65. spyOn(mockMakePaymentState, 'getMakePaymentState$').and.callFake(() => {
  66. return of(makePaymentState);
  67. })
  68. fixture.detectChanges();
  69. });
  70. });

希望这有所帮助!如果您需要任何其他翻译或帮助,请随时告诉我。

英文:

I am trying to write unit test for my angular component.
I am able to mock 2 methods successfully using spyon.
But one method (_paymentStateService.getMakePaymentState$) is giving an error even after I mock and use callFake.
It is giving error (this._payStateService.getMakePaymentState$.subscribe is not a function) in these line:
spyOn(mockMakePaymentState, 'getMakePaymentState$')

  1. fdescribe(&#39;DetailsComponent&#39;, () =&gt; {
  2. let component: DetailsComponent;
  3. let fixture: ComponentFixture&lt;DetailsComponent&gt;;
  4. let httpMock: HttpTestingController;
  5. let formModule: FormsModule;
  6. let form = new FormGroup({
  7. username: new FormControl(&#39;&#39;, Validators.required),
  8. dob: new FormControl(&#39;&#39;)
  9. });
  10. let mockbillingService = {
  11. getDetails: () =&gt; {},
  12. getBankDetails: () =&gt; {}
  13. }
  14. let mockMakePaymentState = {
  15. getPaymentDetails$: () =&gt; of({} as IPaymentDetails),
  16. getDetails: () =&gt; {},
  17. getUserInfo: () =&gt; {}
  18. };
  19. class MatDialogMock {
  20. // When the component calls this.dialog.open(...) we&#39;ll return an object
  21. // with an afterClosed method that allows to subscribe to the dialog result observable.
  22. open() {
  23. return {
  24. afterClosed: () =&gt; of({action: true})
  25. };
  26. }
  27. }
  28. let paymentInfoDetails = &lt;PaymentInfoDetails&gt;{};
  29. let paymentBankDetails = &lt;PaymentBankDetails&gt;{};
  30. let makePaymentState = &lt;IPaymentDetails&gt; {}
  31. paymentInfoDetails.banks = [paymentBankDetails];
  32. beforeEach(async () =&gt; {
  33. await TestBed.configureTestingModule({
  34. declarations: [ ],
  35. imports: [ FormsModule, ReactiveFormsModule],
  36. providers: [
  37. { provide: BillingService, useValue: mockbillingService },
  38. {provide: PayState, useValue: mockMakePaymentState},
  39. { provide: MatDialog, useClass: MatDialogMock }
  40. ]
  41. })
  42. .compileComponents();
  43. fixture = TestBed.createComponent(DetailsComponent);
  44. component = fixture.componentInstance;
  45. httpMock = TestBed.inject(HttpTestingController);
  46. formModule = TestBed.inject(FormsModule);
  47. // fixture.detectChanges();
  48. });
  49. it(&#39;should call ngOnInit&#39;, () =&gt; {
  50. spyOn(mockMakePaymentState, &#39;getDetails&#39;).and.callFake(() =&gt; {
  51. return of(
  52. paymentInfoDetails
  53. );
  54. });
  55. spyOn(mockMakePaymentState, &#39;getBankPayments&#39;).and.callFake(() =&gt; {
  56. return of(
  57. paymentBankDetails
  58. );
  59. });
  60. spyOn(mockMakePaymentState, &#39;getMakePaymentState$&#39;).and.callFake(() =&gt; {
  61. return of(makePaymentState);
  62. })
  63. fixture.detectChanges();
  64. });
  65. });

答案1

得分: 1

不能spyOn实例变量,只能spyOn公共方法。

所以,因此,这行代码是不起作用的:

  1. spyOn(mockMakePaymentState, 'getMakePaymentState$').and.callFake(() => {
  2. return of(makePaymentState);
  3. })

要修复它,你可以这样做:

  1. let mockMakePaymentState = {
  2. getPaymentDetails$: () => of({} as IPaymentDetails),
  3. getDetails: () => {},
  4. getUserInfo: () => {},
  5. // !! 添加这一行,以便将`getMakePaymentState$`模拟为实例变量
  6. getMakePaymentState$: of(makePaymentState),
  7. };
英文:

You can't spyOn instance variables, only public methods.

So therefore, this line won't work:

  1. spyOn(mockMakePaymentState, &#39;getMakePaymentState$&#39;).and.callFake(() =&gt; {
  2. return of(makePaymentState);
  3. })

To fix it, you can do this:

  1. let mockMakePaymentState = {
  2. getPaymentDetails$: () =&gt; of({} as IPaymentDetails),
  3. getDetails: () =&gt; {},
  4. getUserInfo: () =&gt; {},
  5. // !! Add this line so `getMakePaymentState$` is mocked as an instance variable
  6. getMakePaymentState$: of(makePaymentState),
  7. };

huangapple
  • 本文由 发表于 2023年3月7日 03:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定