TheWebs
TheWebs

Reputation: 12933

Why is this ember test failing - Ember JS, Controllers

var model, controller;

// Set up the controller in a new container.
// On tear down, set the variables to null,
// we reset  the app on setup.
module('User Edit Controller', {
    setup: function(){
        AisisPlanner.reset();
        controller = AisisPlanner.__container__.lookup('controller:user_edit');
        model = AisisPlanner.User.store.createRecord('user', {
            name: 'john doe',
            userName: 'john_doe',
            email: '[email protected]',
            bio: '',
            pictureUrl: '',
            role: 'Administrator'
        });
        controller.set('model', model)
    },
    teardown: function(){
      controller = null;
      model = null
    }
});

test('capatalize_user_name', function(){
  model.set('userName', 'John doe')
  equal(controller.get('userName'), 'JOHN DOE', 'all caps');
});

The following test is simple to figure out what were doing, were setting up a controller from the current container and a model. Were then setting information in that model and using it in a test to canalize the name.

The issue is that I am getting the errors:

Setup failed on capatalize_user_name: 'undefined' is not an object (evaluating 'AisisPlanner.User.store.createRecord')

and

Died on test #2 global code@http://localhost:3000/assets/controllers/user_edit_controller_test.js?body=1:26:5: 'undefined' is not an object (evaluating 'model.set')

I got the idea from: this blog post on how to test controllers. I think its mostly out dated but I attempted it none the less. Any ideas how to fix this?

Upvotes: 1

Views: 1367

Answers (2)

TheWebs
TheWebs

Reputation: 12933

So for this test to pass, I had to do the following:

var model, controller;

// Set up the controller in a new container.
// On tear down, set the variables to null,
// we reset  the app on setup.
module('User Edit Controller', {
    setup: function(){
        AisisPlanner.reset();
        Ember.run(function(){
            controller = AisisPlanner.__container__.lookup('controller:user_edit');
            model = controller.get('store').createRecord('user', {
                name: 'Jhon Doe',
                userName: 'John_Doe',
                email: '[email protected]',
                bio: '',
                pictureUrl: '',
                role: 'Administrator'
            });
            controller.set('model', model)
        });
    },
    teardown: function(){
      controller = null;
      model = null
    }
});

test('capatalize_user_name', function(){
    Ember.run(function(){
        model.set('userName', 'JOHN DOE')
        equal(controller.get('userName'), 'JOHN DOE', 'all caps');
    });
});

Ember.run() had to be around both the test and the setup of the controller/model it's self.

The test then had to be changed to SET the model userName as JOHN DOE, as set means you are setting something to it, thus the controller could not be like "oh ya, john doe is capitalized."

Upvotes: 2

Kingpin2k
Kingpin2k

Reputation: 47367

The store doesn't exist on the model Class, but it does on a controller

controller = AisisPlanner.__container__.lookup('controller:user_edit');
model = controller.get('store').createRecord('user', {....

Wrap any failing lines in an Ember run statement (I'm not sure if this is the failing line or not, just an example)

Em.run(function(){
  controller.set('model', model);
});

Upvotes: 0

Related Questions