I saw this error lot of times but any solution for my problem,
my component :
JavaScript
x
35
35
1
*/
2
@Component({
3
selector: 'ngx-modal-result-training',
4
templateUrl: './modal-result-training.component.html',
5
styleUrls: ['./modal-result-training.component.scss'],
6
})
7
export class ModalResultTrainingComponent implements OnInit {
8
@Input() results: IAttempt;
9
@Input() training: ITraining;
10
public validated = false;
11
public opinionForm: FormGroup;
12
public selectedStars = 0;
13
public hasAlreadyComment = true;
14
public percentageScore: number;
15
16
constructor(
17
private opinionsService: OpinionsService,
18
private userService: UserService,
19
private ref: NbDialogRef<ModalResultTrainingComponent>,
20
private router: Router,
21
private toastrService: NbToastrService,
22
private translateService: TranslateService,
23
private dialogService: DialogService
24
) {}
25
26
public ngOnInit(): void {
27
this.dialogService.refs.push(this.ref);
28
this.percentageScore = Math.floor(this.results.score);
29
this.validated = this.percentageScore >= this.training.minimalScore;
30
31
this.checkUserOpinion();
32
this.initForm();
33
}
34
35
My test :
JavaScript
1
99
99
1
const training = {
2
id: 'ZGtz6yrEemCNTo5KAytu',
3
refProject: 'JGvD1faO8L2vWb66BQ87',
4
publicationDate: new Date(),
5
version: 1,
6
name: 'My project',
7
groups: [],
8
category: '',
9
description: '',
10
minimalScore: 80,
11
previewPNG: '',
12
level: 5,
13
gain: 5,
14
fromDate: new Date(),
15
toDate: new Date(),
16
totalSlides: 20,
17
stars: 3,
18
averageStars: 4,
19
comments: 15,
20
groupsHistoric: [],
21
} as ITraining;
22
23
const trainingResults = {
24
id: 'ZDqzqg',
25
version: 1,
26
trainingID: 'xSOvDC6vpZTzVqXy5owQ',
27
userID: 'qdZDZDqg',
28
groupsIDs: [],
29
date: new Date(),
30
time: 10,
31
score: 10,
32
validated: true,
33
finished: true,
34
currentStep: 4,
35
} as IAttempt;
36
37
fdescribe('ModalResultTrainingComponent', () => {
38
let component: ModalResultTrainingComponent;
39
let fixture: ComponentFixture<ModalResultTrainingComponent>;
40
let mockOpinionsService = jasmine.createSpyObj('OpinionService', ['addOpinion']);
41
let mockDialogService = jasmine.createSpyObj('DialogService', ['closeAll', 'refs', 'push']);
42
let mockUserService = jasmine.createSpyObj('UserService', ['user$']);
43
let mockRouter = jasmine.createSpyObj('Router', ['navigate']);
44
let mockToastrService = jasmine.createSpyObj('NbToastrService ', ['success']);
45
let mockTranslateService = jasmine.createSpyObj('TranslateService ', ['instant']);
46
let nbAclService = jasmine.createSpyObj('NbAclService' , ['allow', 'can', 'register', 'setAccessControl']);
47
let nbDialogRef = jasmine.createSpyObj('NbDialogRef', ['push']);
48
49
beforeEach(async () => {
50
await TestBed.configureTestingModule({
51
declarations: [ModalResultTrainingComponent],
52
providers: [
53
{provide : Router, useValue: mockRouter},
54
{provide : OpinionsService, useValue: mockOpinionsService},
55
{provide : UserService, useValue: mockUserService},
56
{provide : DialogService, useValue: mockDialogService},
57
{provide : NbToastrService, useValue: mockToastrService},
58
{provide : TranslateService, useValue: mockTranslateService},
59
{provide : NbDialogRef, useValue: nbDialogRef},
60
EntityService,
61
{provide : NbAclService, useValue : nbAclService},
62
],
63
imports: [
64
FireModule,
65
RouterModule.forRoot([]),
66
NbThemeModule.forRoot(),
67
NbDialogModule.forRoot(),
68
NbAuthModule.forRoot(),
69
TranslateModule.forRoot(),
70
],
71
schemas: [NO_ERRORS_SCHEMA],
72
}).compileComponents();
73
});
74
75
beforeEach(() => {
76
fixture = TestBed.createComponent(ModalResultTrainingComponent);
77
component = fixture.componentInstance;
78
fixture.detectChanges();
79
mockOpinionsService = TestBed.inject(OpinionsService);
80
mockUserService = TestBed.inject(UserService);
81
mockDialogService = TestBed.inject(DialogService);
82
mockRouter = TestBed.inject(Router);
83
mockToastrService = TestBed.inject(NbToastrService);
84
mockTranslateService = TestBed.inject(TranslateService);
85
nbAclService = TestBed.inject(NbAclService);
86
nbDialogRef = TestBed.inject(NbDialogRef);
87
component.training = training;
88
component.results = trainingResults;
89
90
91
});
92
93
it('should create', () => {
94
//expect(component.results.validated).toBeTrue();
95
expect(component).toBeTruthy();
96
});
97
});
98
99
I don’t understand why I got this error : ” TypeError: this.dialogService.refs.push is not a function”
I try to replace useClass instead of useValue but I had unreachable knowing is angular 11. I did lot of research but I found anything.
thanks to all for help
Advertisement
Answer
When you do:
JavaScript
1
2
1
let mockDialogService = jasmine.createSpyObj('DialogService', ['closeAll', 'refs', 'push']);
2
You’re saying that there is a refs
method on DialogService
that I would like to mock but refs
is not a method, it’s an instance variable.
To fix it, I would do this:
JavaScript
1
3
1
let mockDialogService = jasmine.createSpyObj('DialogService', ['closeAll']);
2
mockDialogService.refs = [];
3
Now we attached a refs
property with an empty array and hopefully you shouldn’t see that error anymore. We assigned an empty array so .push
will work.