Angular 测试服务。TypeError:done.fail不是函数
为了测试我的服务,我尝试了以下测试: 它显示了这个错误:Angular 测试服务。TypeError:done.fail不是函数,angular,unit-testing,testing,jasmine,mocking,Angular,Unit Testing,Testing,Jasmine,Mocking,为了测试我的服务,我尝试了以下测试: 它显示了这个错误: TypeError: done.fail is not a function 测试文件 it('should return reasonable json ssss', inject([ProductService, MockBackend], async((service: ProductService, mockBackend: MockBackend) => { const mockResponse = {
TypeError: done.fail is not a function
测试文件
it('should return reasonable json ssss', inject([ProductService, MockBackend], async((service: ProductService, mockBackend: 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.productsgetall().subscribe((facts) => {
console.log(facts)
expect(facts[0].details).toEqual('ffff');
});
})));
-
我的服务
public productsgetall(): Observable<Products[]> {
...
return this.http.get(Api.getUrl(Api.URLS.productsgetall), {
headers: headers
}).map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else {
return res.StatusDescription.map(aa => {
return new Products(aa);
});
}
});
}
如果您有一个异步测试用例,您必须像下面这样使用
done
参数:
it('should return reasonable json ssss', (done) => {
inject([ProductService, MockBackend], async((service: ProductService, mockBackend: MockBackend) => {
// your implementation
service.productsgetall().subscribe((facts) => {
console.log(facts)
expect(facts[0].details).toEqual('ffff');
done(); // <-- this will finish the test
},
(error) => done.fail());
}))(); // <-- important, inject itself returns a function which needs to be called
});
it('应返回合理的json SSS',(完成)=>{
注入([ProductService,MockBackend],异步((服务:ProductService,MockBackend:MockBackend)=>{
//您的实现
service.productsgetall().subscribe((事实)=>{
console.log(事实)
expect(事实[0]。详细信息).toEqual('ffff');
done();//done.fail());
}))();//我自己在尝试测试一个简单的Angular 6服务时遇到了类似的情况,该服务需要http。我不确定潜在的问题是什么,但我怀疑这与将async与inject结合使用有关。当我重构测试以不再使用inject时,这个错误消失了
注意:在Angular的最新版本中,如果您使用的是新的HttpClient,则上面使用的“MockBackend”已被HttpTestingController替换,请参阅media.com上新HttpClient(包括测试)的有趣介绍
考虑到您上面的代码,重构将如下所示。显然,我没有您原始的服务详细信息可供测试。:)
从'@angular/common/http/testing'导入{HttpClientTestingModule,HttpTestingController};
描述('ProductService',()=>{
让httpock:HttpTestingController;
让服务:产品服务;
在每个之前(()=>{
TestBed.configureTestingModule({
导入:[HttpClientTestingModule],
提供者:[产品服务]
});
httpMock=TestBed.get(HttpTestingController);
service=TestBed.get(ProductService);
});
它('应该返回合理的json SSS',异步(()=>{
常量mockResponse={
数据:[
{alarmnumber:0,alarmdesc:'所有的猫都是狮子'},
{alarmnumber:1,alarmdesc:'Video 1'},
{alarmnumber:2,alarmdesc:'Video 2'},
{alarmnumber:3,alarmdesc:'Video 3'},
]
};
service.productsgetall().subscribe(事实=>{
控制台日志(事实);
console.log(事实[0]);
//期望(事实、长度)、托夸尔(300);
expect(事实[0].alarmdesc.toEqual('ffff');
});
让req=httpMock.expectOne('/your/test/url');//当我在帖子中放入}();
like时,show:[ts]无法调用类型缺少调用签名的表达式。类型“Subscription”没有兼容的调用签名。当我没有在帖子中放入}();
like时,结果是:[object ErrorEvent] thrown@site我错过了一个右括号,应该是}();
@site也许你必须添加一个done.fail()
订阅函数的错误处理程序。与mockBackend.connections.subscribe相同。我已经更新了答案以给出一个示例。我用最终代码编辑我的帖子,是否存在以下错误:类型错误:完成。失败不是函数
it('should return reasonable json ssss', (done) => {
inject([ProductService, MockBackend], async((service: ProductService, mockBackend: MockBackend) => {
// your implementation
service.productsgetall().subscribe((facts) => {
console.log(facts)
expect(facts[0].details).toEqual('ffff');
done(); // <-- this will finish the test
},
(error) => done.fail());
}))(); // <-- important, inject itself returns a function which needs to be called
});
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
describe('ProductService', () => {
let httpMock: HttpTestingController;
let service: ProductService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [ProductService]
});
httpMock = TestBed.get(HttpTestingController);
service = TestBed.get(ProductService);
});
it('should return reasonable json ssss', async(() => {
const mockResponse = {
data: [
{ alarmnumber: 0, alarmdesc: 'All cats are lions' },
{ alarmnumber: 1, alarmdesc: 'Video 1' },
{ alarmnumber: 2, alarmdesc: 'Video 2' },
{ alarmnumber: 3, alarmdesc: 'Video 3' },
]
};
service.productsgetall().subscribe(facts=> {
console.log(facts);
console.log(facts[0]);
// expect(facts.length).toEqual(300);
expect(facts[0].alarmdesc).toEqual('ffff');
});
let req = httpMock.expectOne('/your/test/url'); // <-- details not provided in your question
expect(req.request.responseType).toEqual('json');
req.flush(mockResponse); // send mockResponse back into the subscribe above
}));
});