我有这个试图模拟的 Angular 服务:@Injectable({ providedIn: 'root',})export class NotificationsService { private _notifications = new Subject
我正在尝试模拟这个 Angular 服务:
@Injectable({
providedIn: 'root',
})
export class NotificationsService {
private _notifications = new Subject<Notification>();
public notifications: Observable<Notification> = this._notifications.asObservable();
public crudNotifications: Observable<CrudNotification> = this._notifications.asObservable().pipe(
filter(notification => notification.notificationType === 'crud-notification'),
map(notification => notification as CrudNotification)
);
//....
}
我刚刚将其迁移到 Jest,并试图弄清楚当我的测试服务尝试访问时如何模拟返回自定义可观察对象 NotificationsService.crudNotifications
目前,我有这个:
describe('OrganizationUsersService', () => {
let organizationUsersService: OrganizationUsersService;
let crudNotifications!: Subject<CrudNotification>;
let notificationServiceMock!: Spy<NotificationsService>
let storeMock!: Spy<Store>;
let httpTestingController! :HttpTestingController;
beforeEach(()=>{
TestBed.configureTestingModule({
providers:[
{provide: NotificationsService, useValue: createSpyFromClass(NotificationsService)},
{provide: Store, useValue: createSpyFromClass(Store) },
{provide: Router, useValue: createSpyFromClass(Router) },
provideHttpClient(),
provideHttpClientTesting(),
]
})
httpTestingController = TestBed.inject(HttpTestingController);
notificationServiceMock = TestBed.inject<any>(NotificationsService);
storeMock = TestBed.inject<any>(Store);
//Common mocked stuff behavior for every methods here
crudNotifications = new Subject<CrudNotification>();
notificationServiceMock.crudNotifications = crudNotifications.asObservable();
storeMock.dispatch.mockReturnValue(of());
storeMock.select.mockReturnValue(of({ id: 'some-id' }));
//Finally create the service to test:
organizationUsersService = TestBed.inject(OrganizationUsersService);
})
fit('should refresh the store when the backend inform a new role attribution has been added for the current organization id', fakeAsync(() => {
//Arrange
organizationUsersService.initialize(); //Required to register
//Act
crudNotifications.next({
dataType: 'organization.role-attribution',
operationType: 'add',
id: 'some-id',
notificationType: 'crud-notification',
});
tick();
//Assert
httpTestingController.expectOne(environment.backendUrl + 'some/stuff').flush({});
httpTestingController.expectOne(environment.backendUrl + 'some/other/stuff').flush({});
expect(storeMock.dispatch).toHaveBeenCalledWith(expect.any(SetLoadingAction));
organizationUsersService.dispose();
}));
}));
这不起作用,因为我尝试设置的可观察对象没有启用 Spy 方法。我尝试过这个:
notificationServiceMock.crudNotifications.mockReturnValue(crudNotifications);
但公共字段上不存在 mockReturnValue 方法。
有没有办法在公共领域模拟价值?
entryComponents
对于更高版本的角度来说,不再需要。
从所有模块中 entryComponents
删除
将这些组件移至声明数组即可。如果您收到任何错误
Error: src/app/pages/web-filter/categories/categories.component.html:4:7 - error NG8001: 'nb-card-header' is not a known element:
1. If 'nb-card-header' is an Angular component, then verify that it is part of this module.
2. If 'nb-card-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
4 <nb-card-header>
您必须将相应的模块/组件导入到您声明它的模块中,直到不再出现错误。