我试图找出为什么article.service.js 文件测试失败并出现以下错误:
TypeError: Article is not a constructor
17 | */
18 | static async saveArticle(userId, articleObj) {
> 19 | const newArticle = new Article();
但是,我找不到原因。
我将
Article
中的 article.model.js
声明为基于正常模式的正常 Mongoose 模型:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const articleSchema = new Schema(
{
slug: { type: String },
title: { type: String, required: true },
description: { type: String, required: true },
body: { type: String, required: true },
tagList: [String],
favoritesCount: { type: Number, default: 0 },
comments: [{ type: mongoose.Types.ObjectId, ref: 'Comment' }],
author: { type: mongoose.Types.ObjectId, ref: 'User' },
},
{ timestamps: true }
);
const Article = mongoose.model('Article', articleSchema);
module.exports.Article = Article;
然后我在
article.service.js
中加载并使用它:
const { Article } = require('../models/article.model');
const { currentUser } = require('../services/user.service');
const { slugify } = require('../helpers/slugify.helper');
class ArticleService {
static async saveArticle(userId, articleObj) {
const newArticle = new Article();
// ...more code here...
}
}
我在测试中触发(
article.service.test.js
)为:
const slugifyHelper = require('../../../helpers/slugify.helper');
const currentUserService = require('../../../services/user.service');
const { Article } = require('../../../models/article.model');
const mockUser = {
_id: "6368ad4347dkhs",
email: "[email protected]"
};
const mockArticle = {
title: "Sample article title",
description: "Sample article description",
body: "Sample article body",
tagList: ['tag1', 'tag2']
};
// Mock the Article model
jest.mock('../../../models/article.model', () => ({
save: jest.fn().mockResolvedValue(mockUser)
}));
const slugifySpy = jest.spyOn(slugifyHelper, "slugify").mockReturnValue('mocked-slug');
const currentUserSpy = jest.spyOn(currentUserService, "currentUser").mockResolvedValue(mockUser);
describe("Unit tests for article.service.js file", () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe("saveArticle tests", () => {
test("If user can save an article", async () => {
//Arrange
const { saveArticle } = require("../../../services/article.service");
//Act
const result = await saveArticle(mockUser._id, mockArticle);
//Assert
expect(Article).toHaveBeenCalledTimes(1);
expect(slugifySpy).toHaveBeenCalledTimes(1);
expect(slugifySpy).toHaveBeenCalledWith(mockArticle.title);
expect(currentUserSpy).toHaveBeenCalledTimes(1);
expect(currentUserSpy).toHaveBeenCalledWith(mockUser._id);
});
});
});
我想编写用于单元测试 saveArticle 函数的测试套件。在该测试套件中,我尝试测试 slugify 和 currentUser 函数至少被调用一次并带有必要的参数。
您没有正确模拟
article.model.js
模块。
article.model.js
:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const articleSchema = new Schema(
{
slug: { type: String },
},
{ timestamps: true },
);
const Article = mongoose.model('Article', articleSchema);
module.exports.Article = Article;
article.service.js
:
const { Article } = require('./article.model');
class ArticleService {
static async saveArticle(userId, articleObj) {
const newArticle = new Article();
return newArticle.save();
// ...more code here...
}
}
module.exports = ArticleService;
const { Article } = require('./article.model');
const mockUser = {
_id: '6368ad4347dkhs',
email: '[email protected]',
};
const mockArticle = {
title: 'Sample article title',
description: 'Sample article description',
body: 'Sample article body',
tagList: ['tag1', 'tag2'],
};
// Mock the Article model
jest.mock('./article.model', () => {
return {
Article: jest.fn().mockImplementation(() => ({
save: jest.fn().mockResolvedValue(mockUser),
})),
};
});
describe('Unit tests for article.service.js file', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('saveArticle tests', () => {
test('If user can save an article', async () => {
const ArticleService = require('./article.service');
const actual = await ArticleService.saveArticle(mockUser._id, mockArticle);
expect(Article).toHaveBeenCalledTimes(1);
expect(actual).toEqual(mockUser);
});
});
});