Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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
测试Angular2HTTP服务_Angular_Angular2 Services_Angular2 Testing - Fatal编程技术网

测试Angular2HTTP服务

测试Angular2HTTP服务,angular,angular2-services,angular2-testing,Angular,Angular2 Services,Angular2 Testing,我开始学英语了 拥有具有以下实现的服务: @Injectable() export class PostsService { constructor(private http: Http) { } getAllPosts() { return this.http.get('/api/posts') .map(res => res.json()) } } 我正在尝试测试这一点: beforeEach(() => {

我开始学英语了

拥有具有以下实现的服务:

@Injectable()
export class PostsService {

    constructor(private http: Http) { }

    getAllPosts() {
        return this.http.get('/api/posts')
            .map(res => res.json())
    }

}
我正在尝试测试这一点:

beforeEach(() => {
    TestBed.configureTestingModule({
        imports: [HttpModule],
        providers: [
            PostsService,
            { provide: XHRBackend, useClass: MockBackend }
        ]
    })
})

it('should be exposed', inject([PostsService], (service: PostsService) => {
    expect(service).toBeTruthy()
}));

it('should use HTTP calls to obtain data results', 
    inject([PostsService],
        fakeAsync((service: PostsService) => {   
            // test some mocked response data
            // test connection
            // whatever else needs to be tested


        }
)));
很抱歉,这么简单的请求,但我浏览的大多数指南都过时了

请阅读@jornsharpe的博客:

/*tslint:disable:没有未使用的变量*/
进口{
可注射,
反射射束
}从“@angular/core”开始;
进口{
试验台,
伪异步,
注射
}从“@角度/核心/测试”
从“./posts.service”导入{PostsService}
进口{
BaseRequestOptions,
连接后端,
Http,,
答复,,
回应,,
请求方法,
请求选项,
XHRBackend
}来自“@angular/http”
从“@angular/http/testing”导入{MockConnection,MockBackend}
描述('MockBackend PostsService',()=>{
在每个之前(()=>{
this.injector=ReflectiveInjector.resolveAndCreate([
{provide:ConnectionBackend,useClass:MockBackend},
{provide:RequestOptions,useClass:BaseRequestOptions},
Http,,
售后服务
] )
this.postsService=this.injector.get(postsService)
this.backend=this.injector.get(ConnectionBackend)作为MockBackend
} )
它('应该暴露',()=>{
expect(this.postsService).toBeTruthy()
} );
它('应该使用对api/posts URL的HTTP调用',()=>{
这是backend.connections
.subscribe((连接:任意)=>this.lastConnection=连接)
this.postsService.getAllPosts()
期望(this.lastConnection).toBeDefined()
expect(this.lastConnection.request.url).toBe(“/api/posts”)
expect(this.lastConnection.request.method).toEqual(RequestMethod.Get,
'预期的获取请求')
} );
它('应该返回一些数据',完成=>{
设expectedResult=[
{
“用户ID”:1,
“id”:1,
“标题”:“sunt aut”,
“正文”:“quia et”
},
{
“用户ID”:1,
“id”:2,
“标题”:“最简单的”,
“body”:“est rerum”
}
]
this.backend.connections.subscribe((连接:MockConnection)=>{
连接。mockresponse(新响应)(新响应选项({
现状:200,
正文:预期结果
} ) ) )
})
this.postsService.getAllPosts()
.订阅(数据=>{
expect(数据).toEqual(expectedResult)
完成()
}
)
} )
} );

你的问题是什么?你似乎还没有真正写一个测试。你有没有读过,其中有一个例子说明它的用途?我承认,我没有。。我在周末学习这个,希望通过编写和阅读代码来学习。现在读一读,回到问题上来。我写了一篇关于角度测试的博客文章,在向其他组件公开数据的特定方式中,你可能也会发现它很有用:@jornsharpe:Your blog post是我在测试服务中遇到的最好的资源。非常有价值,即使我没有完全观察到雪人的反馈,谢谢;我很高兴你发现它很有用!