Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

javascript - 本身具有依赖关系的间谍服务(spy service which itself has dependencies)

I can't seem to find an example for the following problem.(我似乎找不到以下问题的示例。)

I have a unit test for my NGXS state:(我对NGXS状态进行了单元测试:) describe('Project store', () => { let store: Store; let projectService: ProjectService; beforeEach(async(() => { TestBed.configureTestingModule({ imports: [NgxsModule.forRoot([ProjectState])] }).compileComponents(); projectService = new ProjectService(); // error: needs httpClient store = TestBed.get(Store); })); it('should load projects', () => { const EXPECTED_PROJECTS = [ { "id": "1", "description": "abc" }, { "id": "1", "description": "abc" } ] as Project[]; spyOn(projectService, 'loadProjects').and.returnValue(EXPECTED_PROJECTS); const expected: ProjectStateModel = { loadedProjects: EXPECTED_PROJECTS, createdProjects: [] }; store.dispatch(new LoadProjects()); const actual = store.selectSnapshot(ProjectState.getState); expect(actual).toEqual(expected); }); }); How do I spy on ProjectService when it itself needs the httpClient ?(当ProjectService本身需要httpClient时,如何对其进行httpClient ?) I have found examples on how do test a service which needs the httpClient , but now about how to mock a service that needs the client.(我找到了有关如何测试需要httpClient的服务的httpClient ,但现在找到了有关如何模拟需要客户端的服务的httpClient 。) Now I know I could just write a mock service myself, but I am trying to find out if this can be done using jasmine spies.(现在我知道自己可以自己编写一个模拟服务,但是我试图找出是否可以使用茉莉花间谍程序来完成。)   ask by user3629892 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use NO_ERRORS_SCHEMA inside TestBed.configureTestingModule({}), it will tell angular to ignore unknown elements(您可以在TestBed.configureTestingModule({})中使用NO_ERRORS_SCHEMA ,它将告诉angular忽略未知元素)

TestBed.configureTestingModule({ declarations: [], schemas: [ NO_ERRORS_SCHEMA ] })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...