英文:
Unable to test my code because of the following error: this.instructionsService.instruction.subscribe is not a function , please see the code below:
问题
以下是翻译好的部分:
instruction-context.component.spec.ts
let instructionService: InstructionsService;
let mockInstructionService: = {
instruction: new BehaviorSubject<any>({}).asObservable(),
applyInstruction: () => {},
};
let expectedInstructions = {[]}// object with some expected data
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [InstructionContextComponent],
imports: [HttpClientTestingModule],
providers: [
{ provide: InstructionsService, useValue: mockInstructionService},
HttpService
]
}).compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(InstructionContextComponent);
instructionService = TestBed.inject(InstructionsService);
component = fixture.componentInstance;
fixture.detectChanges();
});
describe('ngOninit', () => {
let instrcutionServiceSpyApplyInstruction;
beforeEach(() => {
instrcutionServiceSpyApplyInstruction = spyOn(instructionService, 'applyInstruction');
spyOn(instructionService , 'instruction' as any).and.returnValue(of(expectedInstructions));
});
it('should create', () => {
// 断言
expect(component).toBeTruthy();
});
it('should apply correct state when mode is "create" and locationData is present',
fakeAsync(() => {
// 准备
component.mode = 'create';
component['locationData'] = [{ id: '1', name: 'Node 1' }] as any;
// 执行
component.ngOnInit();
tick(100);
// 断言
expect(clearInterval).toHaveBeenCalled();
expect(instrcutionServiceSpyApplyInstruction ).toHaveBeenCalled();
}));
})
instructionService.ts
public instruction: BehaviorSubject<Instructions>;
set instructions(){
let state
// 设置一些状态值
this.instruction.next({
...state
});
}
instruction-context.component.ts
ngOninit(){
this.instructionsService.instruction.subscribe((instruction: IInstruction) => {
this.instruction = instruction;
this.instructionLevel = this.instruction && this.instruction['level'];
this.instructionContext = this.instruction && this.instruction['context'];
if (this.instructionContext) {
this.locationData = this.instructionContext['locationData'];
if (this.instructionContext['types'] && this.instructionContext['types'][this.mode]) {
this.filterNodes = this.instructionContext['types'][this.mode][
'filterNodes'
];
this.tree = this.instructionContext['types'][this.mode]['treeData'];
this.selectedDevicesCount = Array.isArray(
this.instructionContext['types'][this.mode]['selectedNodes']
)
? this.instructionContext['types'][this.mode]['selectedNodes'].length
: 0;
}
}
this.validity.emit(this.checkFormValidity());
});
希望这能帮助到您。如果您需要任何进一步的帮助,请告诉我。
英文:
instruction-context.component.spec.ts
let instructionService: InstructionsService;
let mockInstructionService: = {
instruction: new BehaviorSubject<any>({}).asObservable(),
applyInstruction: () => {},
};
let expectedInstructions = {[]}// object with some expected data
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [InstructionContextComponent],
imports: [HttpClientTestingModule],
providers: [
{ provide: InstructionsService, useValue: mockInstructionService},
HttpService
]
}).compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(InstructionContextComponent);
instructionService = TestBed.inject(InstructionsService);
component = fixture.componentInstance;
fixture.detectChanges();
});
describe('ngOninit', () => {
let instrcutionServiceSpyApplyInstruction;
beforeEach(() => {
instrcutionServiceSpyApplyInstruction = spyOn(instructionService, 'applyInstruction');
spyOn(instructionService , 'instruction' as any).and.returnValue(of(expectedInstructions));
});
it('should create', () => {
//Assert
expect(component).toBeTruthy();
});
it('should apply correct state when mode is "create" and locationData is present',
fakeAsync(() => {
// Arrange
component.mode = 'create';
component['locationData'] = [{ id: '1', name: 'Node 1' }] as any;
// Act
component.ngOnInit();
tick(100);
// Assert
expect(clearInterval).toHaveBeenCalled();
expect(instrcutionServiceSpyApplyInstruction ).toHaveBeenCalled();
}));
})
instructionService.ts
public instruction: BehaviorSubject<Instructions>;
set instructions(){
let state
//setting some values for state
this.instruction.next({
...state
});
}
instruction-context.component.ts
ngOninit(){
this.instructionsService.instruction.subscribe((instruction: IInstruction) => {
this.instruction = instruction;
this.instructionLevel = this.instruction && this.instruction['level'];
this.instructionContext = this.instruction && this.instruction['context'];
if (this.instructionContext) {
this.locationData = this.instructionContext['locationData'];
if (this.instructionContext['types'] && this.instructionContext['types'][this.mode]) {
this.filterNodes = this.instructionContext['types'][this.mode][
'filterNodes'
];
this.tree = this.instructionContext['types'][this.mode]['treeData'];
this.selectedDevicesCount = Array.isArray(
this.instructionContext['types'][this.mode]['selectedNodes']
)
? this.instructionContext['types'][this.mode]['selectedNodes'].length
: 0;
}
}
this.validity.emit(this.checkFormValidity());
});
The line I get the error is at this.instructionsService.instruction.subscribe, the first line in my ngOninit() method, in my component and component.ngOnInit() in the spec file. As can be seen in the code 'instruction' is an observable. As per my knowledge ,I have mocked the required dependencies correctly. Not sure what am I missing here. Any help would be highly appreciated!
答案1
得分: 1
尝试删除beforeEach
中的第二行,看看是否有效。我们只能在方法/函数上使用spyOn
,不能用于实例变量如instruction
。
其余部分我觉得没问题。
beforeEach(() => {
instrcutionServiceSpyApplyInstruction = spyOn(instructionService, 'applyInstruction');
// !! 删除这一行
spyOn(instructionService, 'instruction' as any).and.returnValue(of(expectedInstructions));
});
英文:
Try getting rid of the second line in the beforeEach
to see if it works. We can only use spyOn
for methods/functions, not instance variables like instruction
.
The rest looks ok to me.
beforeEach(() => {
instrcutionServiceSpyApplyInstruction = spyOn(instructionService, 'applyInstruction');
// !! Get rid of this line
spyOn(instructionService , 'instruction' as any).and.returnValue(of(expectedInstructions));
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论