I’m just starting to use the meteortesting:mocha package. I’ve looked at some tutorials online, and have a test method that works and passes, but it isn’t really what I through testing should be.
I have a class called alertSeverity
that takes a name and 2 color inputs.
In the method for insert, I also get the userId, current Date, and some other user related information to insert. None of this is passed to the method when called though.
So, from the tutorial, I generate some test data using faker
for name and the 2 colors. Then I just create to const for the other user info.
The issue is this just tests if I can write to the collection.
I have the following in alertSeverity.test.js:
if (Meteor.isServer) {
describe('Add Severity', () => {
it('can add a severity', () => {
const severityName = faker.hacker.adjective(20);
const severityColor = faker.commerce.color();
const severityText = faker.commerce.color();
const severityEntityParent = "Global";
const severityUserEntity = "Global";
AlertSeverity.insert({
severityName,
severityColor,
severityText,
severityEntityParent,
severityUserEntity,
addedBy: "bmcgonag",
addedOn: new Date(),
});
});
});
}
Instead of inserting from inside the test, I figured it made more sense to call my method int he alertSeverity.js file.
my alertSeverity.js method below:
Meteor.methods({
'add.alertSeverity' (severityName, severityColor, textColor) {
check(severityName, String);
check(severityColor, String);
check(textColor, String);
if (!this.userId) {
throw new Meteor.Error('User is not authorized to add alert severity levels. Check to ensure you are logged in.');
}
let userEntity = Meteor.users.findOne(this.userId).profile.usersEntity;
let entityInfo = Entities.findOne({ entityName: userEntity });
let parentEntity = entityInfo.entityParent;
return AlertSeverity.insert({
severityName: severityName,
severityColor: severityColor,
severityText: textColor,
severityEntityParent: parentEntity,
severityUserEntity: userEntity,
addedBy: Meteor.users.findOne(this.userId).username,
addedOn: new Date(),
});
},
and I would then change my test to be:
if (Meteor.isServer) {
describe('Add Severity', () => {
it('can add a severity', () => {
const severityName = faker.hacker.adjective(20);
const severityColor = faker.commerce.color();
const severityText = faker.commerce.color();
Meteor.call('add.alertSeverity', severityName, severityColor, severityText);
});
});
}
When I do this, however, I get an error (which is good) that I’m not logged in, and therefore it can’t run my test. So, it’s good because I want the method to only be accessible when a user is logged in, but I also want to be able to test it.
I guess I’m missing something, but how do I get the test to run if I can’t use the UI to log in?
As always, any help is greatly appreciated.