Unit testing a model using ember-cli


#1

Hey Guys,

I’m new to ember-js. I’m trying to follow a tutorial using ember-cli and not sure what I’m doing wrong.

I get Attempting to register an unknown factory: model:user and I’m not sure why.

Any ideas ?

import { test, moduleForModel } from 'ember-qunit';
import startApp from '../helpers/start-app';



var App;

moduleForModel('user','user tests' {
  setup: function(){
    App = startApp();
  },
  teardown: function(){
    Ember.run(App, 'destroy');
  }
});


test('displayName', function() {
	
	var container = new Ember.Container();
	container.register('store:main', DS.Store);
  	container.register('model:user', App.User);
 
	var store = container.lookup('store:main');
	var user = store.createRecord({
		    firstName: 'Tom',
		    lastName: 'Dale'
		  });
 
  equal(user.get('displayName'), 'Tom Dale');

});

Thanks!


#2

Here’s an example that demonstrates how you can get it working and require’s a bit less boilerplate.

test('It stores title', function () {
	var store = this.store();
	var record = null;
	Ember.run(function() {
		store.createRecord('episode', {id: 1, title: 'Test'});
		record = store.find('episode', 1);
	});
        equal(record.get('title', 'Test');
});

#3

Thanks Martin !

I’ll give it a try!


#4

@mfeckie What about relationships? I’m trying to get this working, but when I use store.createRecord('reated') and set it on my model, it interferes with the store from my acceptance tests.