Meteor/Jasmine/Velocity:如何测试需要登录用户的服务器方法? [英] Meteor / Jasmine / Velocity : how to test a server method requiring logged in user?
问题描述
使用velocity/jasmine,我对如何测试需要当前登录用户的服务器端方法有点困惑.有没有办法让 Meteor 认为用户是通过 stub/fake 登录的?
Using velocity/jasmine, I'm a bit stuck on how I should test a server-side method requiring that there be a currently logged-in user. Is there a way to make Meteor think a user is logged in via stub/fake ?
myServerSideModel.doThisServerSideThing = function(){
var user = Meteor.user();
if(!user) throw new Meteor.Error('403', 'not-autorized');
}
Jasmine.onTest(function () {
describe("doThisServerSideThing", function(){
it('should only work if user is logged in', function(){
// this only works on the client :(
Meteor.loginWithPassword('user','pwd', function(err){
expect(err).toBeUndefined();
});
});
});
});
推荐答案
您可以做的只是将用户添加到您的测试套件中.您可以通过在服务器端测试脚本中填充这些用户来实现:
What you could do is add users just to your test suite. You could do this by populating these users in a the server-side test script:
类似于:
Jasmine.onTest(function () {
Meteor.startup(function() {
if (!Meteor.users.findOne({username:'test-user'})) {
Accounts.createUser
username: 'test-user'
... etc
然后,一个好的策略可能是在测试中使用 beforeAll
登录(这是客户端端):
Then, a good strategy could be to use the beforeAll
in your test to login (this is client side):
Jasmine.onTest(function() {
beforeAll(function(done) {
Meteor.loginWithPassword('test-user','pwd', done);
}
}
这是假设您的测试尚未登录.您可以通过检查 Meteor.user()
并在 afterAll
等中正确注销来使这更有趣.注意如何轻松传递 done
回调到许多 Accounts
函数.
This is assuming your test isn't logged in yet. You can make this more fancy by checking for Meteor.user()
and properly logging out in an afterAll
, etc. Note how you can handily pass the done
callback to many of the Accounts
functions.
本质上,您不必模拟用户.只需确保您在 Velocity/Jasmine DB 中拥有正确的用户和正确的角色.
Essentially, you don't have to mock a user. Just make sure you have the right users, with the correct roles, available in the Velocity/Jasmine DB.
这篇关于Meteor/Jasmine/Velocity:如何测试需要登录用户的服务器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!