mardi 24 février 2015

Can't find method attach to controller with angular mock

I'm trying to implement a test with jasmine. But I have issue with the scope of my controller, I'm using the this syntax and not the $scope syntax.


It looks like this:



TeamMemberCtrl = (teamMemberService) ->
this.getMembersInfos = () ->
self = this
teamMemberService.getMembersInfos().then (response) ->
self.members = response.data.members


TeamMemberCtrl.$inject = ["teamMemberService"]
angular.module("allsquareApp").controller "TeamMemberCtrl",TeamMemberCtrl


In my test I try to call getMembersInfos like this:



beforeEach(module('allsquareApp'))

beforeEach inject (_$httpBackend_, _$compile_, $rootScope, $controller, $location, $injector, $timeout, assetService) ->
@scope = $rootScope.$new()
@assetService = assetService
@http = _$httpBackend_
@compile = _$compile_
@location = $location
@controller = $controller
@injector = $injector
@timeout = $timeout
@model = (name) =>
@injector.get(name)
@eventLoop =
flush: =>
@scope.$digest()
@sandbox = sinon.sandbox.create()

afterEach ->
@http.resetExpectations()
@http.verifyNoOutstandingExpectation()


describe 'TasksController', ->
self = @
beforeEach ->
self.teamCtrl = @controller('TeamMemberCtrl')
console.log(self.teamCtrl)
@response = {
"members": [
{
"imageLink": "landing/patrick_rahme.jpg",
"memberName": "Patrick Rahme",
"memberTitle": "Co-founder/CEO",
"memberLocation": "Luxembourg / San Francisco",
"memberEmailLink": "mailto:patrick@allsquaregolf.com",
"memberDescription": "Patrick's passion for golf, curiosity for technology and attention to detail are at the heart of All Square. As a past national golf champion who speaks 5 languages fluently, Patrick loves to engage with people on and off the golf course.",
"memberEmail": "patrick[at]allsquaregolf.com",
"memberTwitter": "http://ift.tt/1ae9Tng",
"memberLinkedin": "http://ift.tt/1A3LpC2",
"memberGooglep": "http://ift.tt/1ae9Rf9"
}
]
}
# console.log(@assetService.getTeamMembersInfosPath())
@http.whenGET(@assetService.getTeamMembersInfosPath()).respond(200, @response)


describe 'load', ->
it 'sets up the list of current tasks', ->

console.log(self)
self.teamCtrl.getMembersInfos()
@http.flush()


The console log of the teamCtrl, it doesn't contain the definition of my method attached to this in my controller:



function () {
var self;
self = this;
return teamMemberService.getMembersInfos().then(function(response) {
return self.members = response.data.members;
});
}


My error is:



TasksController load sets up the list of current tasks
TypeError: 'undefined' is not a function (evaluating 'self.teamCtrl.getMembersInfos()') in file:///Users/francklavisse/Documents/workspaces/rails-app/spec/javascripts/tmp/assets/app/controllers/teamMemberCtrl_spec.js (line 60)
TypeError: 'undefined' is not a function (evaluating 'self.teamCtrl.getMembersInfos()')


EDIT1


We find that the controller is evaluating self.getMembersInfos, so it return it, that's not what we want



TeamMemberCtrl = function (teamMemberService) {
var self;
self = this;
return self.getMembersInfos = function () {
self = this;
return teamMemberService.getMembersInfos().then(function (response) {
return self.members = response.data.members;
});
};
};

Aucun commentaire:

Enregistrer un commentaire