英文:
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<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;
});
}
}
Unit test:
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();
});
});
答案1
得分: 1
我认为你对targetAccountsForQrCode$
的模拟有误。根据你的代码,targetAccountsForQrCode$
是服务中的一个属性类,而不是一个函数。所以正确的模拟应该是这样的:
const mockThirdPartyTransactionService = { targetAccountsForQrCode$: of({})};
如果targetAccountsForQrCode$
是一个函数,尝试使用jasmine
的createSpyObj
来替代你的模拟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<ThirdPartyTransactionService>(
'ThirdPartyTransactionService',
['targetAccountsForQrCode$']
);
beforeEach(() => mockTransactionService.targetAccountsForQrCode.and.returnValue(of({})))
beforeEach(async() => {
TestBed.configureTestingModule({
declarations: [...],
imports: [...],
providers: [
{
provide: ThirdPartyTransactionService,
useValue: mockTransactionService
}
]
})
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论