Test from Meteor todo tutorial fails:

Hi everyone,

When stepping through the “Testing” part of the tutorial at https://www.meteor.com/tutorials/blaze/testing , using the exact code provided at https://github.com/meteor/simple-todos/blob/9a08b96bae018a4ecb3d23dada624accdb817cb0/imports/api/tasks.tests.js , I am getting the following failed test:

can delete owned task ‣
Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
at AccountsServer.userId (packages/accounts-base/accounts_server.js:82:13)
at Object.Meteor.userId (packages/accounts-base/accounts_common.js:223:19)
at Object.tasksRemove (imports/api/tasks.js:39:31)
at Context. (imports/api/tasks.tests.js:35:20)
at run (packages/practicalmeteor:mocha/meteor/src/setup/SetupRunnables.js:113:18)
at Context.wrappedFunction (packages/practicalmeteor:mocha/meteor/src/setup/SetupRunnables.js:125:16)
at callFnAsync (packages/practicalmeteor:mocha/lib/runnable.js:338:8)
at Test.Runnable.run (packages/practicalmeteor:mocha/lib/runnable.js:290:7)
at Runner.runTest (packages/practicalmeteor:mocha/lib/runner.js:422:10)
at packages/practicalmeteor:mocha/lib/runner.js:533:12

I am guessing this is not what is supposed to happen. Is it possible to explain to a Meteor newbie what is causing this to occur? I’m not even sure which line of the test is invoking Meteor.userID.

EDIT: I posted this too quickly - the offending line is clearly " if (task.owner !== Meteor.userId()) {" in imports/api/tasks.js. But I’m not sure how to address it.

The simple-todos tests should work properly (just re-verified). Maybe compare your code with the completed tutorial code, paying close attention to any differences between your imports/api/tasks.* files. If that doesn’t help it’s possible your build is in an invalid state. Maybe try a meteor reset (usual warning - will wipe your local DB).

Thanks! Replacing imports/api/tasks.js plus meteor reset did it.

The most important cause seems to be that the (correct) completed tutorial code for imports/api/tasks.js contains “this.userId” in every instance where the code on https://www.meteor.com/tutorials/blaze/security-with-methods mentions Meteor.userId(). In retrospect, I should have been able to figure that out from the error message.