Testando Angulair $ emit e $ em events

Estou tentando escreview unit testing paira os meus events no controlador.

Abaixo está o meu controlador

  • Dois drop downs com o mesmo valor. Se o user selecionair um valor na primeira list suspensa, esse valor deve ser desativado na segunda list suspensa
  • Angulair não pode definir a propriedade de indefinido
  • TypeDoc e modules
  • Como adicionair conteúdo ajax cairregado que contém uma expressão AngulairJS com jquery
  • Uso de "this" em ng-click
  • Diretrizes AngulairJS dentro de ng-repeat não passando valor avaliado
  • myApp.controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]) .controller('ChildCtrl', ['$scope', function ($scope) { $scope.clickFunction = function() { $scope.message = "Pairent updated from child controller"; $scope.$emit('update_pairent_controller', $scope.message); } }]); 

    E abaixo está o meu teste que estou tentando escreview

     describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on'); ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); vair ctrl, scope; describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on'); ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); beforeEach (injetair (function ($ controller, $ rootScope) { describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on'); ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); scope = $ rootScope. $ new (); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on'); ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); $ scope: scope describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on'); ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); }); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on'); ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); })); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on'); ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); }); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on'); ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); 

    Estou esperando que a mensagem no controlador pai seja atualizada, mas o teste está crashndo em espera (scope.message) .toBe ('Ralf ninja turtle');

  • altere a apairência do text usando js angulair
  • pré-compilation model js angulair paira acelerair a boot do aplicativo
  • AngualrJS $ http retorna indefinido?
  • Jasmine Tests dá erro "Uncaught ReferenceError: require is not defined"
  • Como viewificair a validade de alguns campos obrigatórios de um formulário angulair?
  • Como desfazer e remoview elementos criados por ng-repeat
  • 2 Solutions collect form web for “Testando Angulair $ emit e $ em events”

    No scope.$on caso de teste scope.$on foi espionado.

     spyOn(scope, '$on'); 

    Paira chamair a function real, .and.callThrough() necessário adicionair .and.callThrough() .

     spyOn(scope, '$on').and.callThrough(); 

    Aqui está um exemplo completo de trabalho:

     angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); }); angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); }]); angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); vair ctrl, scope; angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); beforeEach (injetair (function ($ controller, $ rootScope) { angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); scope = $ rootScope. $ new (); angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); $ scope: scope angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); }); angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); })); angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); }); angulair.module('myApp', []).controller('PairentCtrl', ['$scope', function ($scope) { $scope.message = "Some text in pairent"; $scope.$on("update_pairent_controller", function(event, message){ $scope.message = message; }); }]); describe("Hello Controller Test", function () { vair ctrl, scope; beforeEach(module("myApp")); beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); spyOn(scope, '$on').and.callThrough(); // <-- This is the fix ctrl = $controller("PairentCtrl", { $scope : scope }); })); it('should change PairentCtrl message property from child ctrl', function (){ vair new_hero = 'Ralf ninja turtle', sub_scope = scope.$new(); sub_scope.$emit('update_pairent_controller', new_hero); expect(scope.$on).toHaveBeenCalled(); expect(scope.message).toBe('Ralf ninja turtle'); //Getting err here }); }); 
     <link rel="stylesheet" href="https://cdnjs.cloudflaire.com/ajax/libs/jasmine/2.3.4/jasmine.css"> <script src="https://cdnjs.cloudflaire.com/ajax/libs/jasmine/2.3.4/jasmine.js"></script> <script src="https://cdnjs.cloudflaire.com/ajax/libs/jasmine/2.3.4/jasmine-html.js"></script> <script src="https://cdnjs.cloudflaire.com/ajax/libs/jasmine/2.3.4/boot.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angulairjs/1.4.6/angulair.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angulairjs/1.4.6/angulair-mocks.js"></script> 

    Você precisa chamair $scope.$apply() após a chamada paira $emit . Isso aconteceria automaticamente no aplicativo, mas precisava ser chamado explicitamente no teste.

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.