Is this currently the best way to *unit* test Ember models w/ Mocha?


#1

I am just getting started using Ember. Coming from an Angular background, I really want to use BDD (Mocha) and Karma for test-running. Anyhow, I was very confused about how to create some simple model instances for some simple unit tests, and came up with the following. I am wondering if this is considered best practice until the ember-mocha-adapter project gets a little better with regard to helpers? Thanks for any insight.

/* global describe, it, beforeEach, afterEach */
(function(app) {

    'use strict';

    describe('MyModel Model', function() {
        var store;

        beforeEach(function() {
            app.reset();
            store = app.__container__.lookup('store:main');
        });

        describe('#init', function() {

            it('should create an id', function() {
            
                Ember.run(function() {
                    var model = store.createRecord(app.MyModel, {
                        someProp: 'something'
                    });
                    model.get('id').should.exist;
                });
            
            });

        });

    });

})(window.MyEmberApp);

#2

Hi @chiefy,

first when starting ember, take a look at ember-cli if you haven’t already.

2nd if you want to use mocha and karma, there are some tricky parts, but based on some research I did for my project, it’s possible to hook mocha via tedd zeeny’s ember-mocha-adapter

I’ve tried to get karma working as well but the effort replacing testem was too much time consuming (I am running the test runner only for CI). The idea of ember-cli is now it will processors all your raw code, ‘compile’ it into a production like code and run the tests on it. If there should be hooked karma somewhere it could cause a perf bottleneck.

this simple mocha example was useful as well for me.

In case of further questions, do not hesitate to reply or write a pm to me.