gpt4 book ai didi

需要身份验证服务的 GET 请求的 Angular Testing

转载 作者:行者123 更新时间:2023-12-05 07:42:06 25 4
gpt4 key购买 nike

我是 Karma/Jasmine 测试的新手,我正在尝试为执行 GET 请求的服务设置测试。我将 XHRBackend 与 MockBackend 对象一起使用来模拟虚假请求和响应。我有一个执行 GET 的“webService”和一个保存连接字符串和访问 token 的“authService”。我已将此测试设置如下:

describe('WebService Tests', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
AuthService,
AdalService,
WebServicesService,
{ provide: XHRBackend, useClass: MockBackend }
]
})
.compileComponents();
}));

beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) => {
backend = be;
authService = new AuthService(new AdalService(), http);
webService = new WebServicesService(http, authService);
fakeWebServicesData = makeServicesData;
let webOptions = new ResponseOptions({status: 200, body: {data: fakeWebServicesData}})
webResponse = new Response(webOptions);
}));

it('should have expected successful GET of WebServices', async(inject([], () => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(webResponse));

webService.getWebServices().toPromise()
.then(data => {
expect(data.length).toBe(fakeWebServicesData.length,
'should have received all web services data');
})

})))

我收到的响应是失败的,因为 data.length 是未定义的,它应该是 1(我使用的假数据的长度是 1)。我有点不确定为什么会这样。它调用的方法如下:

getWebServices(): Observable<WebService[]> {
let connection = this.authService.connection + '/services';
// Add authorization header with jwt token
let token = this.authService.getToken();
let headers = new Headers({ 'Authorization': 'Bearer ' + token });
let options = new RequestOptions({ headers: headers });

// Get services from web service api endpoint
return this.http.get(connection, options)
.map((response: Response) => response.json())
.catch((error: Response) => {
return Observable.throw(error);
});
}

我怀疑是因为在发出模拟请求之前我还没有使用 AuthService 登录,所以它可能返回未定义。在设置此模拟请求之前,我是否需要使用我的 authService 登录? (例如,事先进行模拟登录?)如果是这样,我如何在同一测试中连续执行两个模拟请求?还是有其他方法来伪造身份验证?任何帮助,将不胜感激。非常感谢。

更新:这是我的假数据

const makeServicesData = [
{
name: "service1", version: "1.0.0", versionPublishedBy: "",
creationTime: "", snapshotId: "", runtimeType: "",
initCode: "", code: "", description: "", operationId: "",
inputParameterDefinitions: [
{ name: "", type: "" }
],
outputParameterDefinitions: [
{ name: "", type: "" }
],
outputFileNames: [""],
myPermissionOnService: "read/write"
}
]

最佳答案

您应该将预期更改为 data.data.length。您命名为数据的结构将包含 { data: [ ...actual data ] }。我还建议将其重命名为 response 或其他名称。但是你能验证这是否有效吗?:

it('should have expected successful GET of WebServices', async(inject([], () => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(webResponse));

webService.getWebServices().toPromise()
.then(data => {
expect(data.data.length).toBe(fakeWebServicesData.length,
'should have received all web services data');
})

})))

关于需要身份验证服务的 GET 请求的 Angular Testing ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44837020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com