Error: Expected one matching request for criteria "Match method: GET, URL: /users", found none
Error: Expected one matching request for criteria "Match method: GET, URL: /users", found none
我有一个非常简单的服务调用和一个 jasmine 测试。
服务电话:
getUserInfo() {
return this.http.get('/users');
}
测试方法:
it('should get user info', inject([UsersService, HttpTestingController], (service: UsersService, backend: HttpTestingController) => {
const testUser = [{username: 'testLogin', firstName: 'testFName', lastName: 'testLName'}];
service.getUserInfo().subscribe( user => {
expect(user).toEqual(testUser);
});
backend.expectOne({
method: 'GET',
url: '/users'
}).flush(testUser);
}));
我做错了什么?为什么业力说错误:
Error: Expected one matching request for criteria "Match method: GET, URL: /users", found none.
这样试试
进口{
HttpClientTestingModule,
HttpTestingController
} 来自“@angular/common/http/testing”;
import { ApiService } from "./api.service";
describe("ApiService", () => {
let httpMock: HttpTestingController;
let service: ApiService;
const api_server="http://localhost:8080";
beforeEach(() =>
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
declarations: [],
providers : [ApiService]
})
);
beforeEach(() => {
service = TestBed.get(ApiService);
httpMock = TestBed.get(HttpTestingController);
});
it("should be created", () => {
expect(service).toBeTruthy();
});
it("should test getUserInfo()", () => {
const testUser = [{username: 'testLogin', firstName: 'testFName', lastName: 'testLName'}];
service.getUserInfo().subscribe(res => {
expect(res).toEqual(testUser);
});
const req = httpMock.expectOne(
`${api_server}/users`
);
expect(req.request.method).toEqual("GET");
req.flush(testUser);
httpMock.verify();
});
});
我有一个非常简单的服务调用和一个 jasmine 测试。
服务电话:
getUserInfo() {
return this.http.get('/users');
}
测试方法:
it('should get user info', inject([UsersService, HttpTestingController], (service: UsersService, backend: HttpTestingController) => {
const testUser = [{username: 'testLogin', firstName: 'testFName', lastName: 'testLName'}];
service.getUserInfo().subscribe( user => {
expect(user).toEqual(testUser);
});
backend.expectOne({
method: 'GET',
url: '/users'
}).flush(testUser);
}));
我做错了什么?为什么业力说错误:
Error: Expected one matching request for criteria "Match method: GET, URL: /users", found none.
这样试试
进口{ HttpClientTestingModule, HttpTestingController } 来自“@angular/common/http/testing”;
import { ApiService } from "./api.service";
describe("ApiService", () => {
let httpMock: HttpTestingController;
let service: ApiService;
const api_server="http://localhost:8080";
beforeEach(() =>
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
declarations: [],
providers : [ApiService]
})
);
beforeEach(() => {
service = TestBed.get(ApiService);
httpMock = TestBed.get(HttpTestingController);
});
it("should be created", () => {
expect(service).toBeTruthy();
});
it("should test getUserInfo()", () => {
const testUser = [{username: 'testLogin', firstName: 'testFName', lastName: 'testLName'}];
service.getUserInfo().subscribe(res => {
expect(res).toEqual(testUser);
});
const req = httpMock.expectOne(
`${api_server}/users`
);
expect(req.request.method).toEqual("GET");
req.flush(testUser);
httpMock.verify();
});
});