【发布时间】:2020-06-19 21:58:16
【问题描述】:
所以我正在尝试测试这段代码
src/helpers/CommentHelper.ts:
export default class CommentHelper {
gitApiObject: GitApi.IGitApi ;
constructor(gitApiObject: GitApi.IGitApi)
{
this.gitApiObject = gitApiObject;
}
async postComment(commentContent: string, repoId: string, pullRequestId: number): Promise<any> {
const comment: GitInterfaces.Comment = <GitInterfaces.Comment>{content: commentContent};
const newCommentThread: GitInterfaces.GitPullRequestCommentThread = <GitInterfaces.GitPullRequestCommentThread>{comments: [comment]}
await this.gitApiObject.createThread(newCommentThread, repoId, pullRequestId);
}
}
这里是测试:
import CommentHelper from "../helpers/CommentHelper";
import { mocked } from 'ts-jest/utils';
import { GitApi, IGitApi } from "azure-devops-node-api/GitApi";
jest.mock('../helpers/CommentHelper', () => {
return {
default: jest.fn().mockImplementation(() => {})
};
});
describe("CommentHelper Tests", () => {
const mockedGitApi = mocked(GitApi, true);
beforeEach(() => {
mockedGitApi.mockClear();
});
it("Check to see if the gitApiObject is called properly", () => {
const commentHelper = new CommentHelper(<any>mockedGitApi);
const spy = jest.spyOn(GitApi.prototype ,'createThread')
commentHelper.postComment("", "", 0);
expect(spy).toHaveBeenCalled();
})
})
这是错误:
TypeError: commentHelper.postComment is not a function
23 | const commentHelper = new CommentHelper(<any>mockedGitApi);
24 | const spy = jest.spyOn(GitApi.prototype ,'createThread')
> 25 | commentHelper.postComment("", "", 0);
| ^
26 | expect(spy).toHaveBeenCalled();
27 | })
28 |
现在我们处于项目的早期阶段,所以测试非常简单。我们只想确保调用了 gitApiObject/createThread。如何在不显式模拟 postComment 函数的情况下实现这一点?
谢谢! :)
【问题讨论】:
标签: javascript node.js typescript jestjs ts-jest