unit-testingember.jsmockingember-cliember-testing

Ember testing with Objects that use third party scripts


I wish to test some Ember Objects/Models which make use of a third party script for some calculations.

When I began writing these tests, I hit a problem when I started interacting with the third party scripts as they weren't loaded and I hit a bunch of undefined functions. I can get round it in this case by overriding the function in the test setup but this felt horrible.

Coming from a C# background I would normally use a mock/stub for the third party library and inject it as a depedency. During testing I would just check the mock was called correctly rather than testing the output.

I couldn't find a sensible example of this in Ember.

Would mocking the library be the most appropriate approach in Ember unit testing? and if that's the case, does anyone have a suggestion from a sensible mocking library I can use for Ember unit testing? I'm tied to an old version of Ember unfortunately for this project 1.7 (cli v 0.1.2).


Solution

  • Yes, we are also using mocking practices. For example; creating a mock service, a mock util and even creating a mock config file...

    For me, I'm not using an external library to mock objects. (By the way, as similar with you, I'm a java developer.) As written in the guides, I do stubbing of the needed object.

    You can have a look at other answers at Stackoverflow about stubbing: stubbing store at component tests, stubbing store at instance initializer tests...

    Your idea of mocking is totally valid and reasonable. If you want further research, you can have a look at mocking tools section of the qunit's plugins.