错误:预期对标准“按功能匹配:"的一个匹配请求,未找到 [英] Error: Expected one matching request for criteria "Match by function: ", found none
问题描述
我有一个要测试的userService.spec.ts
文件,代码以这种方式运行.它调用了调用ADMIN_API_URL
的get函数.我尝试测试给定的文件,但标题中提到了错误.
我的 user.service.spec.ts 文件.
I have a userService.spec.ts
file that I want to test.The code goes this way. It calls a get function that calls the ADMIN_API_URL
.I tried testing the given file but got an error that is mentioned in the title.
My user.service.spec.ts file.
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});
});
});
我尝试测试我脑海中所有的东西,但仍然找不到我的代码出了什么问题.我在做任何愚蠢的错误吗?
I tried testing all the things that I had in my mind but still can't find what is wrong with my code.Am I doing any silly mistake?.
推荐答案
很可能URL不匹配. expectOne
条件(request.url === `${url}search/users?q=name`)
可能失败.您可以记录您的模拟请求URL来查看实际的URL.
Most likely the URLs are not matching. The expectOne
condition (request.url === `${url}search/users?q=name`)
is likely failing. You can log your mock request URL to see what the actual URL is.
const req = httpMock.expectOne(request => {
console.log("url: ", request.url);
return true;
});
这篇关于错误:预期对标准“按功能匹配:"的一个匹配请求,未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!