【问题标题】:Angular 1.5 test component with jasmine带有茉莉花的Angular 1.5测试组件
【发布时间】:2016-08-28 21:25:49
【问题描述】:

我尝试使用 Jasmine 和 Angular-mock 测试我的组件,但我不知道这是怎么做的。

这是我的组件

var angular = require('angular');
'use strict';
module.exports = angular
    .module('app.login.component.login', [])
    .component('login', {
      templateUrl: '/app/js/login/components/login.template.html',
      controller: LoginController
    });

LoginController.$inject = ['$state', 'Auth', 'messages'];
function LoginController($state, Auth, messages) {
  var ctrl = this;
  ctrl.failMessage = messages.NO_AUTH;
  ctrl.failResponse = false;
  ctrl.login = login;

  function login(user){
    ctrl.errors = {};
    Auth.login(user)
      .success(function(result){
        $state.go('profile');
      })
      .error(function(response) {
        ctrl.failResponse = true;
    })
  };
}

我写了这个测试,但他不工作。 请解释一下我做错了什么并展示一些如何测试组件的模式

describe('Component: login', function() {
  beforeEach(angular.mock.module(require('angular-ui-router')));
  beforeEach(angular.mock.module(loginComponent.name));

  var scope;
  beforeEach(inject(function($rootScope, $compile){
    scope = $rootScope.$new();
  }));

   var controller;
   beforeEach(inject(function($componentController, Auth) {
     ctrl = $componentController('login', {

      $scope:scope});
   }));

   it('df', function() {
     expect(ctrl.login).toBeDefined();
   });
});

【问题讨论】:

    标签: javascript angularjs unit-testing jasmine


    【解决方案1】:

    你使用$componentController

    beforeEach(inject(function($rootScope, $componentController){
      scope = $rootScope.$new();
      controller = $componentController('myComponent', {$scope: scope});
    }));
    

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      相关资源
      最近更新 更多