Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 角度4:使用mockRespond和RxJS可观测值_Ajax_Angular_Testing_Service - Fatal编程技术网

Ajax 角度4:使用mockRespond和RxJS可观测值

Ajax 角度4:使用mockRespond和RxJS可观测值,ajax,angular,testing,service,Ajax,Angular,Testing,Service,我最近构建了一个有效的应用程序,我正在尝试构建一个测试。我的服务从API后端获取项目: 出口级CatfactService{ 构造函数私有http:http{} 获取事实{ 常量url=http://www.catfact.info/api/v1/facts.json; 返回this.http.geturl.mapthis.extractData .catch this.handleError; } 这是规范的固定版本。主要问题是没有导入角度响应 非常感谢,杰。这正是问题所在。我真的很感谢你的

我最近构建了一个有效的应用程序,我正在尝试构建一个测试。我的服务从API后端获取项目:

出口级CatfactService{ 构造函数私有http:http{} 获取事实{ 常量url=http://www.catfact.info/api/v1/facts.json; 返回this.http.geturl.mapthis.extractData .catch this.handleError;
} 这是规范的固定版本。主要问题是没有导入角度响应


非常感谢,杰。这正是问题所在。我真的很感谢你的帮助!
            import { TestBed, inject, fakeAsync, tick } from '@angular/core/testing';

            import { CatfactService } from './catfact.service';
            import { HttpModule, Http, BaseRequestOptions, XHRBackend, ResponseOptions, Response, RequestOptions } from '@angular/http';
            import { MockBackend } from '@angular/http/testing';
            describe('CatfactService', () => {
                beforeEach(() => {
                    TestBed.configureTestingModule({
                        imports: [HttpModule],
                        providers: [
                            CatfactService,
                            MockBackend,
                            BaseRequestOptions,
                            {
                                provide: Http,
                                useFactory: (backend, options) => new Http(backend, options),
                                deps: [MockBackend, BaseRequestOptions]
                            }
                        ]
                    });
                });

                it('should return reasonable json', inject([CatfactService, MockBackend], fakeAsync((service: CatfactService, mockBackend) => {

                    const mockResponse = {
                        data: [
                            { id: 0, details: 'All cats are lions' },
                            { id: 1, details: 'Video 1' },
                            { id: 2, details: 'Video 2' },
                            { id: 3, details: 'Video 3' },
                        ]
                    };

                    mockBackend.connections.subscribe(connection => {
                        connection.mockRespond(new Response(
                            new ResponseOptions({
                                body: [
                                    { id: 0, details: 'All cats are lions' },
                                    { id: 1, details: 'Video 1' },
                                    { id: 2, details: 'Video 2' },
                                    { id: 3, details: 'Video 3' },
                                ]
                            })));
                    });

                    service.getFacts().subscribe((facts) => {
                        expect(facts.length).toBe(4);
                        expect(facts[0].details).toEqual("All cats are lions");
                    });

                    tick();
                })));
            });