Angular单元测试管道不是一个函数

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

Angular unit test pipe is not a function

问题

我在运行单元测试时遇到了错误 this.targetAccounts$.pipe 不是一个函数,我不确定需要添加什么。

类:

export class GenerateQrCodeComponent implements OnInit {
    
    targetAccounts$: Observable<IBWAccount[]>;

    constructor(private thirdPartyTransactionService: ThirdPartyTransactionService) {}

    ngOnInit() {
        this.targetAccounts$ =
            this.thirdPartyTransactionService.targetAccountsForQrCode$;
        this.targetAccounts$.pipe(first()).subscribe((accounts) => {
            this.handleTargetChange(accounts[0]);
        });
        this.targetAccounts$.subscribe((accounts) => {
            this.targetAccounts = accounts;
        });
    }
}

单元测试:

describe('GenerateQrCodeComponent', () => {
    let component: GenerateQrCodeComponent;
    let fixture: ComponentFixture<GenerateQrCodeComponent>;

    const mockThirdPartyTransactionService = {
        targetAccountsForQrCode$: () => of({}),
    };

    beforeEach(waitForAsync(() => {
        TestBed.configureTestingModule({
            declarations: [
                GenerateQrCodeComponent,
            ],
            imports: [
                TranslateModule.forRoot(),
                ReactiveFormsModule,
                FormsModule,
                BrowserAnimationsModule,
                IonicModule,
            ],
            providers: [
                {
                    provide: ThirdPartyTransactionService,
                    useValue: mockThirdPartyTransactionService,
                },
            ],
        }).compileComponents();

        fixture = TestBed.createComponent(GenerateQrCodeComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    }));

    it('should create', () => {
        expect(component).toBeTruthy();
    });
});
英文:

I am getting the error this.targetAccounts$.pipe is not a function when running the unit test and I am not sure what I need to add.

class:

export class GenerateQrCodeComponent implements OnInit {       

targetAccounts$: Observable&lt;IBWAccount[]&gt;; 

constructor(private thirdPartyTransactionService: ThirdPartyTransactionService) {}
            
 ngOnInit() { 
      this.targetAccounts$ =
           this.thirdPartyTransactionService.targetAccountsForQrCode$;
      this.targetAccounts$.pipe(first()).subscribe((accounts) =&gt; {
           this.handleTargetChange(accounts[0]);
      });
      this.targetAccounts$.subscribe((accounts) =&gt; {
            this.targetAccounts = accounts;
      }); 
   }
}

Unit test:

describe(&#39;GenerateQrCodeComponent&#39;, () =&gt; {
    let component: GenerateQrCodeComponent;
    let fixture: ComponentFixture&lt;GenerateQrCodeComponent&gt;;

const mockThirdPartyTransactionService = {
        targetAccountsForQrCode$: () =&gt; of({}),
    };

beforeEach(waitForAsync(() =&gt; {
        TestBed.configureTestingModule({
            declarations: [
                GenerateQrCodeComponent,
            ],
            imports: [
                TranslateModule.forRoot(),
                ReactiveFormsModule,
                FormsModule,
                BrowserAnimationsModule,
                IonicModule,
            ],
            providers: [
                {
                    provide: ThirdPartyTransactionService,
                    useValue: mockThirdPartyTransactionService,
                },
            ],
        }).compileComponents();

        fixture = TestBed.createComponent(GenerateQrCodeComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    }));

    it(&#39;should create&#39;, () =&gt; {
        expect(component).toBeTruthy();
    });
});

答案1

得分: 1

我认为你对targetAccountsForQrCode$的模拟有误。根据你的代码,targetAccountsForQrCode$是服务中的一个属性类,而不是一个函数。所以正确的模拟应该是这样的:

const mockThirdPartyTransactionService = { targetAccountsForQrCode$: of({})};

如果targetAccountsForQrCode$是一个函数,尝试使用jasminecreateSpyObj来替代你的模拟mockThirdPartyTransactionService,方法如下:

const mockTransactionService = 
 jasmine.createSpyObj<ThirdPartyTransactionService>(
  'ThirdPartyTransactionService', 
  ['targetAccountsForQrCode$']
);

beforeEach(() => mockTransactionService.targetAccountsForQrCode$.and.returnValue(of({})))

beforeEach(async() => {
  TestBed.configureTestingModule({
   declarations: [...],
   imports: [...],
   providers: [
     { 
       provide: ThirdPartyTransactionService, 
       useValue: mockTransactionService 
     }
   ]
  })
})
英文:

I think you are mocking targetAccountsForQrCode$ wrong, based on your code, targetAccountsForQrCode$ is a property class from the service, not a function, so mocking it could be like this:

const mockThirdPartyTransactionService = { targetAccountsForQrCode$: of({})};

If targetAccountsForQrCode$ is a function, try replacing your mock mockThirdPartyTransactionService using createSpyObj from jasmine like the following:

const mockTransactionService = 
 jasmine.createSpyObj&lt;ThirdPartyTransactionService&gt;(
  &#39;ThirdPartyTransactionService&#39;, 
  [&#39;targetAccountsForQrCode$&#39;]
);

beforeEach(() =&gt; mockTransactionService.targetAccountsForQrCode.and.returnValue(of({})))

beforeEach(async() =&gt; {
  TestBed.configureTestingModule({
   declarations: [...],
   imports: [...],
   providers: [
     { 
       provide: ThirdPartyTransactionService, 
       useValue: mockTransactionService 
     }
   ]
  })
})

huangapple
  • 本文由 发表于 2023年2月24日 00:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547779.html
匿名

发表评论

匿名网友

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

确定