Meteor-stubs spyOn Accounts not working when testing package

Hi there,

I am facing a weird behavior and I hope you could help me on it.

I am testing my package using sanjo:jasmine@0.20.2 and velocity:meteor-stubs@1.1.0’
In my tests I do:

beforeEach(function () {
    MeteorStubs.install();
});

Works well for Meteor.users.

But when using spyOn on Accounts it doesn’t work.
If I do this in my test method:

spyOn(Accounts, 'setPassword').and.callFake(function() {
  // do nothing
});
var result = MyService.changePassword(user, newPassword);
expect(Accounts.setPassword).toHaveBeenCalled();

html-reporter reports:

Expected spy setPassword to have been called.

Any idea ?
Thanks for your help.
Yann

Hi,

I have reported my issue on stack overflow http://stackoverflow.com/questions/33075729/meteor-stubs-spyon-accounts-not-working-when-testing-package

regs,
Yann

What’s inside MyService.changePassword?

A simple call:

Accounts.setPassword(user._id, password);

Let’s try to take a look inside. Can you try to console.log(Accounts.setPassword) before the call to MyService.changePassword and then again before the call to Accounts.setPassword inside the service?

console.log(Accounts.setPassword) result before service call:

{ [Function]
   future: [Function],
   async: [Function],
   asyncApply: [Function],
   and: 
    { identity: [Function],
      exec: [Function],
      callThrough: [Function],
      returnValue: [Function],
      returnValues: [Function],
      throwError: [Function],
      callFake: [Function],
      stub: [Function] },
   calls: 
    { track: [Function],
      any: [Function],
      count: [Function],
      argsFor: [Function],
      all: [Function],
      allArgs: [Function],
      first: [Function],
      mostRecent: [Function],
      reset: [Function] } }

console.log(Accounts.setPassword) result before Accounts.setPassword(…) call:

 [Function]

Empty, like if the spyOn() was never called !?

Hmm

The stubs package creates an empty function as can be seen here.

Are you explicitly calling the MeteorStubs.install somewhere? Perhaps you don’t even need the stubs at all since you’re manually controlling the dependencies with spies

I’m calling MeteorStubs.install() from the beforeEach() statement and MeteorStubs.uninstall() from the afterEach() statement like so:

describe('Testing', function () {
	beforeEach(function () {
		MeteorStubs.install();
	});

	afterEach(function () {
		MeteorStubs.uninstall();
	});

       // Testing parts below
}

I will try without to see the result.

The result is : Accounts is not defined

Ok, solved the issue :smile:

Thanks to stackoverflow http://stackoverflow.com/questions/32058367/meteor-referenceerror-accounts-is-not-defined

Use api.imply to give your app access to the exported symbols of your package dependencies.

I have added it in my package.js file:

 api.imply([
    'accounts-password'
  ]);

And everything works ok now :smile:

1 Like