如何解决Angular Jasmine + Contentful:如何测试 Contentful 调用?
我正在使用 Contentful 将文本加载到我的 Angular 单页应用程序中。它工作正常,但现在我想测试它。该测试实际上应该调用 Contentful 并从我的空间加载 myPage 的文本。然后我想断言预期值(myPage 的标题字段)不为空。 这样我想确保从我的前端/SPA 调用 Contentful 工作。
我调用 Contentful 的代码放在名为 [Warning] Aborted connection 1101 to db: 'unconnected' user: 'unauthenticated' host: '127.0.0.1' (This connection closed normally without authentication)
的服务中。这是我为此服务尝试的测试:
contentful.service.spec
contentful.service.ts
这是我的contentful.service.ts:
describe('ContentfulService',() => {
let service: ContentfulService;
//Arrange
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ContentfulService);
});
it('should get myPage for the default language en-US',(done) => {
//Act
service.getMyPage("en-US").then(myPage => {
//Assert
expect(myPage.fields.title.length).toBeGreaterThan(0);
done();
});
}
正如我所说,这段代码工作正常,只有测试失败并出现以下错误之一:
export class ContentfulService {
private clientConsentPage = createClient({
space: environment.contentfulConsent.spaceId,accessToken: environment.contentfulConsent.access_token,environment: environment.contentfulConsent.environment
});
constructor() { }
public getMyPage(locale: string): Promise<Entry<any>> {
return this.clientConsentPage.getEntries(Object.assign({
content_type: 'myPage'
},{ 'sys.id': environment.contentfulConsent.contentTypeIds.myPage,locale: locale }))
.then(res => res.items[0]);
}
或
TypeError: clientConsentPage.getEntries is not a function
(我不知道为什么它并不总是相同的错误...)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。