[英]Undefined Es6 class in unit test with gulp mocha
我在運行我無法弄清楚我做錯了什么的單元測試時遇到以下參考錯誤的麻煩。
首先,我定義了以下類:
/**
* Invitation class
*/
export default class Invitation {
/**
* Create a Invitation
* @param {string} user The user id
* @param {string} creationDate The creation date of the invitation
* @param {string} validationDate The validation date of the invitation
* @param {string} denyDate The deny date of the invitation
* @param {string} project The project id
* @param {string} userTargetEmail The email address where the invitation was send
* @param {string} _id The invitation _id
*/
constructor(user, project, userTargetEmail, creationDate, validationDate, denyDate, _id) {
/** @type {string} */
this.user = user;
/** @type {string} */
this.creationDate = creationDate;
/** @type {string} */
this.validationDate = validationDate;
/** @type {string} */
this.denyDate = denyDate;
/** @type {string} */
this._id = _id;
/** @type {string} */
this.project = project;
/** @type {string} */
this.userTargetEmail = userTargetEmail;
}
}
然后是以下單元測試文件(問題在每個掛鈎之前出現):
import Mongoose from 'mongoose';
import InvitationController from '../../../../../app/business/invitations/controller/InvitationController';
import InvitationService from '../../../../../app/business/invitations/service/InvitationService';
import ErrorList from '../../../../../app/helper/ErrorList';
import NotificationService from '../../../../../app/helper/service/NotificationService';
import MailService from '../../../../../app/helper/service/mail/op/MailService';
import RegexService from '../../../../../app/helper/service/RegexService';
import UserService from '../../../../../app/business/user/service/UserService';
import User from '../../../../../app/business/user/model/User';
import ProjectService from '../../../../../app/business/projects/service/ProjectService';
import Project from '../../../../../app/business/projects/model/Project';
import UtilService from '../../../../../app/helper/service/UtilService';
import Invitation from '../../../../../app/business/invitations/model/Invitation';
let chai = require('chai');
let sinon = require('sinon');
let expect = chai.expect;
/** @test {InvitationController} */
describe('InvitationController ', () => {
let controller;
let mailService;
let notificationService;
let invitationService;
let regexService;
let userService;
let projectService;
let utilService;
let mockMailService;
let mockNotifService;
let mockInvService;
let mockProjectService;
let mockUserService;
let project;
let userId;
let projectId;
let invitationId;
let user;
beforeEach(() => {
invitationId = Mongoose.Types.ObjectId('4edd40c86762e0fb12000007');
projectId = Mongoose.Types.ObjectId('4edd40c86762e0fb12000005');
userId = Mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
mailService = new MailService();
notificationService = new NotificationService();
invitationService = new InvitationService();
regexService = new RegexService();
userService = new UserService();
projectService = new ProjectService();
utilService = new UtilService();
mockMailService = sinon.mock(mailService);
mockNotifService = sinon.mock(notificationService);
mockInvService = sinon.mock(invitationService);
mockProjectService = sinon.mock(projectService);
mockUserService = sinon.mock(userService);
console.log('Loggin Invitation ', Invitation);
let invitation = new Invitation(userId, projectId, 'mail@mail.fr', 'creationdate', null, null, invitationId);
console.log('logging invit ', invitation);
project = new Project('name', 'creationDate', 'lastModificationDate', 'user', projectId);
user = new User('mail', 'password', 'firstname', 'lastname', '1');
controller = new InvitationController(invitationService, projectService, regexService, mailService, notificationService, userService, utilService);
});
/// all tests
事情變糟了,當我運行測試時,我得到:
InvitationController
addInvitationAction
Loggin Invitation function Invitation(user,project,userTargetEmail,creationDate,validationDate,denyDate,_id){__cov_Thbl4xwtVLP9GaZCsEH5nQ.f['2']++;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['6']++;_classCallCheck(this,Invitation);__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['7']++;this.user=user;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['8']++;this.creationDate=creationDate;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['9']++;this.validationDate=validationDate;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['10']++;this.denyDate=denyDate;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['11']++;this._id=_id;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['12']++;this.project=project;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['13']++;this.userTargetEmail=userTargetEmail;}
1) "before each" hook for "Expect to return an error when projectService.getProjectFromId fail"
...
/// ALL OTHER TESTS LOGS
...
1) InvitationController "before each" hook for "Expect to return an error when projectService.getProjectFromId fail":
ReferenceError: Invitation is not defined
at new Invitation (C:\Users\gayard\Documents\Dev\MYPROJECT\src\app\business\invitations\model\Invitation.js:9:684)
at Context.<anonymous> (C:/Users/gayard/Documents/Dev/MYPROJECT/src/test/unit/business/invitations/controller/TestInvitationController-spec.js:64:22)
at callFn (C:\Users\gayard\Documents\Dev\MYPROJECT\node_modules\gulp-mocha\node_modules\mocha\lib\runnable.js:315:21)
at Hook.Runnable.run (C:\Users\gayard\Documents\Dev\MYPROJECT\node_modules\gulp-mocha\node_modules\mocha\lib\runnable.js:308:7)
at next (C:\Users\gayard\Documents\Dev\MYPROJECT\node_modules\gulp-mocha\node_modules\mocha\lib\runner.js:298:10)
at Immediate._onImmediate (C:\Users\gayard\Documents\Dev\MYPROJECT\node_modules\gulp-mocha\node_modules\mocha\lib\runner.js:320:5)
導入路徑都是有效的(我已經仔細檢查了它們,並使用了一個atom插件來確保它們是正確的)。
起初,我懷疑控制器中的導入錯誤,但實際上並沒有到達,因為它甚至沒有記錄'logging invit'+邀請。
我在應用程序中有多個其他類導入,它們以完全相同的方式進行操作而沒有任何麻煩,而且我不知道發生了什么。 我什至嘗試重命名/重寫它,但是沒有機會。
關於為何邀請函未定義的任何想法,即使我在調用new Invitation()
之前能夠將其記錄下來?
經過幾個小時的測試,我得出結論:
請勿在類名中使用“ Invit”字符串。 我不知道為什么,但是在我的設置中,我不斷收到參考錯誤。 我嘗試了許多不同的名稱,“ Invi”(有效),“ Yolo”(有效),“ Invit”(無效),“ InvitationModel”(無效),並且清單繼續...
最后,在重寫之后,該類現在稱為Call ass following:
/**
* Call class
* (invitation model, CAN NOT be named invitation, it's generating References Errors in the unit tests)
*/
export default class Call {
/**
* Create a Call
* @param {string} user the id of the user sending the invitation
* @param {string} creationDate the creation date of the invitation
* @param {string} validationDate the validation date of the invitation
* @param {string} denyDate the deny date of the invitation
* @param {string} project the project id for wich the invitation has been sent
* @param {string} userTargetEmail the email address of the target
* @param {string} _id the notification's id
*/
constructor(user, creationDate, validationDate, denyDate, project, userTargetEmail, _id) {
/** @type {string} */
this.user = user;
/** @type {string} */
this.creationDate = creationDate;
/** @type {string} */
this.validationDate = validationDate;
/** @type {string} */
this.denyDate = denyDate;
/** @type {string} */
this.project = project;
/** @type {string} */
this.userTargetEmail = userTargetEmail;
/** @type {string} */
this._id = _id;
}
}
這可行。 不過,我仍然不知道為什么,所以,如果有人碰巧對此有一個答案,我會很高興聽到它,因為這是一種不令人滿意的解決方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.