Meteor / Jasmine / Velocity : how to test a server method requiring logged in user?

前端 未结 4 1720
萌比男神i
萌比男神i 2021-02-15 17:50

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

4条回答
  •  盖世英雄少女心
    2021-02-15 18:27

    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:

    Something like:

    Jasmine.onTest(function () {
      Meteor.startup(function() {
        if (!Meteor.users.findOne({username:'test-user'})) {
           Accounts.createUser
              username: 'test-user'
      ... etc
    

    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);
      }
    }
    

    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.

    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.

提交回复
热议问题