gpt4 book ai didi

javascript - 对象没有方法loginWithPassword Meteorjs mocha 测试

转载 作者:行者123 更新时间:2023-12-03 09:53:51 33 4
gpt4 key购买 nike

您好,我正在尝试对我的 meteor 应用程序进行 Mocha 测试。我的代码如下用于测试。

describe("logged in admin insertion test", function() {
before(function(done) {
Meteor.loginWithPassword('admin@admin.com', 'password', done)
})
data ={
item: 'something new'
}
it("Should allow inserting data", function(done) {
chai.expect(Meteor.call.bind(Meteor, 'addNewMenu', data)).to.not.throw(Error);
done();
})
})

我读到loginWithPassword仅在meteor客户端可用。但我看到很少有这样的例子。

由于 loginWithPassword 是异步的,所以我也添加了异步版本

describe("logged in admin insertion test", function() {
before(function(done) {
Meteor.loginWithPassword('admin@admin.com', 'password', function(err, res) {
data ={
item: 'something new'
}
it("Should allow inserting data", function(done) {
chai.expect(Meteor.call.bind(Meteor, 'addNewMenu', data)).to.not.throw(Error);
done();
})
})
})
})

还有其他方法可以模拟登录用户吗?我在这里做错了什么吗?谢谢您的帮助

最佳答案

是的,有。检查一下:

服务器端:

Meteor.methods({
impersonate: function(userId) {
check(userId, String);

if (!Meteor.users.findOne(userId))
throw new Meteor.Error(404, 'User not found');
if (!Meteor.user().isAdmin)
throw new Meteor.Error(403, 'Permission denied');

this.setUserId(userId);
}
});

客户端

Meteor.methods({
impersonate: function(userId) {
check(userId, String);

if (!Meteor.users.findOne(userId))
throw new Meteor.Error(404, 'User not found');
if (!Meteor.user().isAdmin)
throw new Meteor.Error(403, 'Permission denied');

this.setUserId(userId);
}
});

来源:https://dweldon.silvrback.com/impersonating-a-user

关于javascript - 对象没有方法loginWithPassword Meteorjs mocha 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30801293/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com