[英]Meteor & Mocha Chai : test insert function
我正在嘗試測試我的插入功能,但UserAccount失敗:錯誤:無法讀取未定義的屬性“用戶名”
我不知道如何通過測試才能插入帖子,這是我的方法:
Meteor.methods({
'posts.insert'(title, content) {
check(title, String);
check(content, String);
if (! this.userId) {
throw new Meteor.Error('not-authorized');
}
const urlSlug = getSlug(title);
Posts.insert({
title,
content,
urlSlug,
createdAt: new Date(),
owner: this.userId,
username: Meteor.users.findOne(this.userId).username,
});
},
});
這是我要測試的測試方法:
if (Meteor.isServer) {
describe('Posts', () => {
describe('methods', () => {
const userId = Random.id();
let postId;
beforeEach(() => {
Posts.remove({});
postId = Posts.insert({
title: 'test post',
content: 'test content',
urlSlug: 'test-post',
createdAt: new Date(),
owner: userId,
username: 'toto',
});
});
// TEST INSERT METHOD
it('can insert post', () => {
const title = "test blog 2";
const content = "test content blog 2";
const insertPost Meteor.server.method_handlers['posts.insert'];
const invocation = { userId };
insertPost.apply(invocation, [title, content]);
assert.equal(Posts.find().count(), 2);
});
});
});
}
請問你能幫幫我嗎 ?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.