帮助请测试该服务。我发现了很多有关测试的信息,但没有一个没有帮助。
admin.service.ts
private bcUrl = 'http://30.30.0.101:8080';
private httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': this.cookieService.get('token')
})
};
constructor(
private http: HttpClient,
private cookieService: CookieService,
private userService: UserService
) {
}
getUsers(page, lim): any {
return this.http.get(`${this.bcUrl}/api/security/users?page=${page}&size=${lim}`, this.httpOptions)
.subscribe(
(data: DataInterface) => {
this.users = data.content;
this.st.totalElements = data.totalElements;
this.st.totalPages = data.totalPages;
this.dataSource = data.content;
},
error => {
if (error.status === 404) {
alert('Сторінка не знайдена');
}
if (error.status === 401) {
alert('Немає прав доступу');
}
});
}
这是代码的一部分。如果您能进行测试,我将非常高兴。或发送到所需的资源。
这是我的考试。我开始写它,但是我不知道要去哪里。
import {async, getTestBed, inject, TestBed} from '@angular/core/testing';
import {CookieService} from 'ngx-cookie-service';
import {AdminService} from './admin.service';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {UserService} from '@app/core/user/user.service';
import {RequestMethod, ResponseOptions} from '@angular/http';
describe('AdminService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AdminService, CookieService, UserService]
});
});
it('should be created', inject([AdminService], (service: AdminService) => {
expect(service).toBeTruthy();
}));
it('should be getUsers', async(() => {
const adminService: AdminService = getTestBed().get(AdminService);
mockBackend.connections.subscribe(connection => {
expect(connection.request.method).toBe(RequestMethod.Delete);
connection.mockRespond( new Response(new ResponseOptions()))
});
}));
});
答案 0 :(得分:1)
您可以尝试这个
it('should be getUsers', inject([AdminService, HttpTestingController],
(service: AdminService, backend: HttpTestingController) => {
service.getUsers(0, 1);
backend.expectOne({
method: 'GET',
url: 'http://30.30.0.101:8080/api/security/users?page=0&size=1'
}).flush(mockUsers);
expect(service.users).toBe(mockUsers.content);
}));
it('should not getUsers on error status: 404', inject([AdminService, HttpTestingController],
(service: AdminService, backend: HttpTestingController) => {
service.getUsers(0, 1);
backend.expectOne({
method: 'GET',
url: 'http://30.30.0.101:8080/api/security/users?page=0&size=1'
})
.flush(null, { status: 404, statusText: 'Пользователи системы Not Found' });
expect(service.users).toBeFalsy();
}));
it('should not getUsers on error status: 401', inject([AdminService, HttpTestingController],
(service: AdminService, backend: HttpTestingController) => {
service.getUsers(0, 1);
backend.expectOne({
method: 'GET',
url: 'http://30.30.0.101:8080/api/security/users?page=0&size=1'
})
.flush(null, { status: 401, statusText: 'Unauthorized' });
expect(service.users).toBeFalsy();
}));