I’m trying to test my package that needs a value from Meteor.settings
. But when I start the test, I get this error:
Exception while invoking method 'sendData' TypeError: Cannot read property 'appKey' of undefined
My test:
it('sends event data in proper form', function () {
Meteor.settings = {
myApp: {
appKey: 'thisisafakeappkey',
},
};
const post = sinon.stub(HTTP, 'post');
trackEvent(event, data, user);
HTTP.post.restore();
}
The method:
Meteor.methods({
'sendData'({ payload }) {
const appKey = Meteor.settings.myapp.appKey;
if (!appKey) throw new Meteor.Error('No app key found in Meteor.settings.');
const withAppKey = Object.assign({ appKey }, payload);
HTTP.post(getRemoteUrl(), {
data: withAppKey,
}, (err, res) => {
if (err) console.log('err', err);
else console.log('res', res);
});
},
});
can you use sinon to stub the settings as well?
I thought Sinon only stubbed methods? Could you provide an example? And I thought a simple reassignment of a global variable would work with Meteor.settings
Hmm looking at your test I think you’re doing an integration test (i.e. client to server) am I guessing right? I guess you’d need to do something with chimp setup (I am not sure I haven’t gotten chimp to work correctly myself). I would recommend you think of it a different way if you’re unit testing, how about you just call the method directly?
import { resetDatabase } from 'meteor/xolvio:cleaner'
import ValidationError from 'meteor/mdg:validation-error'
import { assert, expect } from 'chai'
import { Tasks } from '/imports/api/tasks'
import './insertTask.run'
/* eslint-env mocha */
/**
* Collection tests. This can only be done on the server side because client
* tests do not have access to the MongoDB collection itself. Note that
* the operations will be done with a `userId = undefined` as these are run
* in the server context.
* @test
*/
describe('collection test', () => {
beforeEach(() => {
resetDatabase()
})
it('can see a collection', () => {
assert(Tasks, 'unable to see sample collection')
})
This file has been truncated. show original
Can you show us how your test and the sendData
method relate to each other? Your code doesn’t show an association (does trackEvent
call sendData
?).