Error: Expected one matching request for criteria "Match by function: ", found none
Error: Expected one matching request for criteria "Match by function: ", found none
我有一个 userService.spec.ts
文件,我想要 test.The 代码按此方式进行。它调用一个调用 ADMIN_API_URL
的 get 函数。我尝试测试给定的文件,但出现了标题中提到的错误。
我的 user.service.spec.ts 文件。
describe('Service: User service', () => {
let httpMock: HttpTestingController;
let userService: UserService;
let url: string;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{ provide: AUTH_API_URL, useValue: 'https://auth.example.com/api/'
},
{ provide: SSO_API_URL, useValue: 'https://sso.example.com/auth/api/' },
{ provide: WIT_API_PROXY, useValue: 'https://wit.example.com/api/'},
{ provide: ADMIN_API_URL, useValue: 'https://admin.example.com/api/'},
{ provide: REALM, useValue: 'realm' },
Broadcaster,
Logger,
AuthenticationService,
HttpHandler,
UserService
]
});
httpMock = TestBed.get(HttpTestingController);
url = TestBed.get(ADMIN_API_URL);
userService = TestBed.get(UserService);
});
it('Get user by user name returns valid user', (done) => {
const testUser = [{
'attributes': {
'fullName': 'name',
'imageURL': '',
'username': 'myUser'
},
'id': 'userId',
'type': 'userType'
}];
userService.getUsersByName('name').subscribe((user) => {
expect(user[1].id).toEqual('userId');
done();
});
const req = httpMock.expectOne(request => request.method === 'GET'
&& request.url === `${url}search/users?q=name`);
req.flush({data: testUser});
});
});
我试着测试了我脑海中的所有东西,但仍然找不到我的问题 code.Am 我犯了什么愚蠢的错误?
很可能 URL 不匹配。 expectOne
条件 (request.url === `${url}search/users?q=name`)
可能失败。您可以记录您的模拟请求 URL 以查看实际的 URL 是什么。
const req = httpMock.expectOne(request => {
console.log("url: ", request.url);
return true;
});
在我的例子中,被测试的服务依赖于我嘲笑的其他一些服务。模拟依赖项错过了被测试服务调用的模拟函数。这个错误被默默地吞噬了。
添加模拟函数后,它开始正常工作。
编辑:您描述的问题可能是由“无声”问题引起的。我的经验如下。
首先检查您的服务依赖性,例如 AuthenticationService
等。
您创建的请求可能会在管道中进一步与这些依赖项交互,并且可能 在 甚至到达 Angular 的 HTTP 客户端之前就被拒绝!
这样请求就不会被 HttpTestingController
注册,您将在测试中得到 found none。
我有一个 userService.spec.ts
文件,我想要 test.The 代码按此方式进行。它调用一个调用 ADMIN_API_URL
的 get 函数。我尝试测试给定的文件,但出现了标题中提到的错误。
我的 user.service.spec.ts 文件。
describe('Service: User service', () => {
let httpMock: HttpTestingController;
let userService: UserService;
let url: string;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{ provide: AUTH_API_URL, useValue: 'https://auth.example.com/api/'
},
{ provide: SSO_API_URL, useValue: 'https://sso.example.com/auth/api/' },
{ provide: WIT_API_PROXY, useValue: 'https://wit.example.com/api/'},
{ provide: ADMIN_API_URL, useValue: 'https://admin.example.com/api/'},
{ provide: REALM, useValue: 'realm' },
Broadcaster,
Logger,
AuthenticationService,
HttpHandler,
UserService
]
});
httpMock = TestBed.get(HttpTestingController);
url = TestBed.get(ADMIN_API_URL);
userService = TestBed.get(UserService);
});
it('Get user by user name returns valid user', (done) => {
const testUser = [{
'attributes': {
'fullName': 'name',
'imageURL': '',
'username': 'myUser'
},
'id': 'userId',
'type': 'userType'
}];
userService.getUsersByName('name').subscribe((user) => {
expect(user[1].id).toEqual('userId');
done();
});
const req = httpMock.expectOne(request => request.method === 'GET'
&& request.url === `${url}search/users?q=name`);
req.flush({data: testUser});
});
});
我试着测试了我脑海中的所有东西,但仍然找不到我的问题 code.Am 我犯了什么愚蠢的错误?
很可能 URL 不匹配。 expectOne
条件 (request.url === `${url}search/users?q=name`)
可能失败。您可以记录您的模拟请求 URL 以查看实际的 URL 是什么。
const req = httpMock.expectOne(request => {
console.log("url: ", request.url);
return true;
});
在我的例子中,被测试的服务依赖于我嘲笑的其他一些服务。模拟依赖项错过了被测试服务调用的模拟函数。这个错误被默默地吞噬了。
添加模拟函数后,它开始正常工作。
编辑:您描述的问题可能是由“无声”问题引起的。我的经验如下。
首先检查您的服务依赖性,例如 AuthenticationService
等。
您创建的请求可能会在管道中进一步与这些依赖项交互,并且可能 在 甚至到达 Angular 的 HTTP 客户端之前就被拒绝!
这样请求就不会被 HttpTestingController
注册,您将在测试中得到 found none。