Ember testing this.store.find hooked up to Asp.net webapi 2 using Ember's template on nuget for Todo Lists Application


#1

Hey guys,

I have been struggling to setup unit tests for my application controller and I was wondering if you could give me some guidance.

In case your wondering, yes I have spent countless hours reading different posts describing how to do this. But I haven’t been able to crack it and I was wondering if you kind folks might be able to give me some hints.

I am using the Nuget Ember Asp.Net Todo items application template as starter code.

In a nutshell: I am using Ember data and WebApi Restful adapter for data plumbing, therefore my backend application returns the following POJSO array: [{"$id":“1”,“userId”:2,“userName”:“hp\NewtonT2”,“resourceManager”:true,“projectManager”:false}]

For url: http://dev_server:1234/api/user/ (Get)

I am stumped because I can never get window.App.ApplicationRoute.setupController to fire off…?!

So I tried this:

           Ember.run(function () {
                var myModel = new Ember.Object();
                var user = new Ember.Object();
                user.set('resourceManager', true);
                myModel.set('content', Ember.A([user]));
                var t = App.ApplicationController.create({
                    model: myModel
                });
                var test = t.get('model');
                console.log(test.get('content')[0].get('resourceManager'));
                console.log(t.canEdit)
            });

Oddly t.canEdit is always undefined…

In my controller I have defined canEdit as:

window.App.ApplicationController = Ember.Controller.extend({
   //
    // Logged in user's user name
    loginUserName: function () {
        return (this.get('model').get('content'))[0].get('userName');
    }.property(),
    //
    // Can current user edit the data?
    canEdit: function () {
        return (this.get('model').get('content'))[0].get('resourceManager');
    }.property()
});

How can I adequately test the ApplicationController and retrieve its two properties using mocks/stubs to isolate the dependencies?

Thanks in advance.


#2

So I spent the weekend banging my head against my keyboard and pulling out what remaining hair I had on top of my head… I think I made some progress.

Here is my test code:

	    // in an attempt to minimize boiler plate type code, use this helper
            containerHelper = function (app, key) {
                return app.__container__.lookup(key);
            };


            var applicationController = App.ApplicationController.create();
            var applicationRoute = App.ApplicationRoute.create();
            
            Ember.run(function () {
                var store = containerHelper(window.App, "store:main").createRecord("user", { $id: 1, id:1, userid: 1, projectManager: 1, error: 'error', resourceManager: 'true' });
                store.resourceManager = true;
                applicationRoute.setupController(applicationController, store)
                console.log(applicationController.get('canEdit'));
            });

I am happy to note that canEdit is finally returning true. However I had to make the following modifications inside of my route:

	window.App.ApplicationController = Ember.Controller.extend({
		//
		// Properties
		//
		// Logged in user's user name
		loginUserName: function () {
			return (this.get('model').get('content'))[0].get('userName');
		}.property(),
		//
		// Can current user edit the data?
		canEdit: function () {
			var model = this.get('model');
			var content = model.get('content');
			if (content != undefined)
			{
				return (content)[0].get('resourceManager');
			}
			else
			{
				return model.get('resourceManager')
			}
		}.property()
	});

So, I am thinking that since I am using the webapi json service to return data as an IEnumerable which translates to the single array element at index 0.

However, I am confused why webapi is returning the model inside of content and further why its at element 0?

I was expecting it to be similar to the way I mocked inside my test… Any ideas?

Thanks.