1

I'm trying to test a component, which has a constructor with multiple parameters. In the testing file (spec file), I've created a componentInstance using:

componentObject: fixture.componentInstance;

Also, I'm using this object to call a function that is defined in the component that prints a console like 'this function has been called'. But, when I do something like this:

it('checking console', async(() => {
spyOn(console, 'log');
componentObject.onClickFunction();
expect(console.log).toHaveBeenCalledWith('this function has been called');
}));

but it gives me an error saying:

Uncaught Expected spy log to have been called with [ 'this function has been called' ] but it was never called

so I'm guessing the function is not getting executed, though I have called it using 'componentObject.onClickFunction()'.

UPDATE: the onClickFunction() literally does nothing except printing logs,

onClickFunction() {
  console.log('this function has been called');
}

Any suggestions?

Aiguo
  • 3,416
  • 7
  • 27
  • 52
  • Could you post your entire spec code? – Fabio Antunes Oct 31 '16 at 17:10
  • Can you show the `onClickFunction` – Paul Samsotha Nov 01 '16 at 01:38
  • just updated the answer! – Aiguo Nov 01 '16 at 01:43
  • If that is all that is in there, it should work. If the console.log is being invoked in an async callback, then it won't work. Other than that, I'll need to see a complete test case, as it works fine for me. Also don't forget to use `@`username when you want to ping somebody in the comments. – Paul Samsotha Nov 01 '16 at 02:15
  • @peeskillet, do you mind sharing your test real quick? I tried it again but still it gives me the same error. On the other hand, if I declare an object with something like "componentObject = new componentClass()" (by calling the class contructor), and call the function onClickFunction(), it works fine and prints the logs in the console. – Aiguo Nov 01 '16 at 14:59
  • I deleted it. But if you wanna show yours, I can check it out to see if you're doing something wrong – Paul Samsotha Nov 01 '16 at 15:37

0 Answers0