Jasmine TypeScript 测试 Angular
Jasmine TypeScript test Angular
几天来,我一直在尝试用 Type Script 为我的应用程序编写一些 Angular 控制器测试,但没有任何成功。在我开始之前我想提一下这是我第一次写茉莉花测试。我的问题是我无法模拟依赖服务来测试控制器。任何帮助或指出正确的方向都会很好。
Angular 模块:
angular.module('some.module', ['ui.router', 'ngAnimate', 'ui.bootstrap', 'pascalprecht.translate', 'ngCookies', 'smart-table']);
Angular 控制器:
module App.Controllers {
export class TestController{
static $inject = ["$scope","SomeService"];
WhatController: () => string;
constructor(private $scope: App.IAppScope, private SomeService) {
var vm = this;
vm.WhatController = function (): string {
return SomeService.someAction();
};
}
}
angular.module("some.module").controller("TestController", TestController);
}
Angular 服务:
module App.Services {
export class SomeService{
httpService: ng.IHttpService;
static $inject = ["$http"];
someAction: () => any;
constructor($http: ng.IHttpService) {
var service = this;
service.someAction= () => {
return "test";
}
}
}
factory.$inject = ['$http'];
function factory($http: ng.IHttpService) {
return new SomeService($http);
}
angular.module('some.module').factory('SomeService', factory);
}
业力文件:
module.exports = function (config) {
config.set({
basePath: 'Scripts',
frameworks: ['jasmine', 'jasmine-matchers'],
files: [
{ pattern: 'angular.js', included: true },
{ pattern: 'angular-mocks.js', included: true },
{ pattern: 'angular-ui-router.js', included: true },
{ pattern: 'angular-ui/ui-bootstrap-tpls.js', included: true },
{ pattern: 'angular-animate.js', included: true },
{ pattern: 'angular-translate.js', included: true },
{ pattern: 'angular-translate-loader-url.js', included: true },
{ pattern: 'angular-cookies.js', included: true },
{ pattern: 'smart-table.js', included: true },
'../app/app.module.js',
'../app/pages/**/*.controller.js',
//Here are controller files
{ pattern: '../app/pages/**/*.spec.js', included: true },
],
exclude: ['**/*min.js'],
preprocessors: {
'../app/pages/**/*.controller.js': ['coverage'],
},
reporters: ['progress', 'coverage'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: true,
coverageReporter: {
type : 'html',
dir : 'coverage/'
}
})
}
测试:
'use strict';
describe("complaints.controller.test", () => {
var $http: ng.IHttpService;
var mockSomeServices: App.Services.SomeServices;
var mock = angular.mock;
beforeEach(() => {
mock.module('ui.router');
mock.module('ngAnimate');
mock.module('ui.bootstrap');
mock.module('pascalprecht.translate');
mock.module('ngCookies');
mock.module('smart-table');
});
//This don't work mockSomeServices is undefined
beforeEach(mock.inject((_$http_, $injector) => {
$http = _$http_;
mockSomeServices = $injector.get('SomeServices');
}));
//This also don't work mockSomeServices is undefined
beforeEach(mock.inject((_$http_, $injector, SomeServices) => {
$http = _$http_;
mockSomeServices = SomeServices;
}));
});
如果你想模拟一个服务,你这样做:
angular.mock.module(($provide: ng.auto.IProvideService): void => {
$provide.constant("SomeService", SomeService = { });
});
在你的控制器中,我认为你不想模拟你的服务。你想知道它被调用了,你可以通过在你的服务上放置一个间谍来做到这一点。
spyOn(SomeService, "someAction");
如果你想让它return一些数据到控制器中use/validate,你可以这样做:
spyOn(SomeService, "someAction").and.returnValue(/* data */);
然后您断言它已被调用并且数据是您所期望的:
expect(SomeService.someAction).toHaveBeenCalled();
expect(yourController.someValue).toEqual("someValue");
几天来,我一直在尝试用 Type Script 为我的应用程序编写一些 Angular 控制器测试,但没有任何成功。在我开始之前我想提一下这是我第一次写茉莉花测试。我的问题是我无法模拟依赖服务来测试控制器。任何帮助或指出正确的方向都会很好。
Angular 模块:
angular.module('some.module', ['ui.router', 'ngAnimate', 'ui.bootstrap', 'pascalprecht.translate', 'ngCookies', 'smart-table']);
Angular 控制器:
module App.Controllers {
export class TestController{
static $inject = ["$scope","SomeService"];
WhatController: () => string;
constructor(private $scope: App.IAppScope, private SomeService) {
var vm = this;
vm.WhatController = function (): string {
return SomeService.someAction();
};
}
}
angular.module("some.module").controller("TestController", TestController);
}
Angular 服务:
module App.Services {
export class SomeService{
httpService: ng.IHttpService;
static $inject = ["$http"];
someAction: () => any;
constructor($http: ng.IHttpService) {
var service = this;
service.someAction= () => {
return "test";
}
}
}
factory.$inject = ['$http'];
function factory($http: ng.IHttpService) {
return new SomeService($http);
}
angular.module('some.module').factory('SomeService', factory);
}
业力文件:
module.exports = function (config) {
config.set({
basePath: 'Scripts',
frameworks: ['jasmine', 'jasmine-matchers'],
files: [
{ pattern: 'angular.js', included: true },
{ pattern: 'angular-mocks.js', included: true },
{ pattern: 'angular-ui-router.js', included: true },
{ pattern: 'angular-ui/ui-bootstrap-tpls.js', included: true },
{ pattern: 'angular-animate.js', included: true },
{ pattern: 'angular-translate.js', included: true },
{ pattern: 'angular-translate-loader-url.js', included: true },
{ pattern: 'angular-cookies.js', included: true },
{ pattern: 'smart-table.js', included: true },
'../app/app.module.js',
'../app/pages/**/*.controller.js',
//Here are controller files
{ pattern: '../app/pages/**/*.spec.js', included: true },
],
exclude: ['**/*min.js'],
preprocessors: {
'../app/pages/**/*.controller.js': ['coverage'],
},
reporters: ['progress', 'coverage'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: true,
coverageReporter: {
type : 'html',
dir : 'coverage/'
}
})
}
测试:
'use strict';
describe("complaints.controller.test", () => {
var $http: ng.IHttpService;
var mockSomeServices: App.Services.SomeServices;
var mock = angular.mock;
beforeEach(() => {
mock.module('ui.router');
mock.module('ngAnimate');
mock.module('ui.bootstrap');
mock.module('pascalprecht.translate');
mock.module('ngCookies');
mock.module('smart-table');
});
//This don't work mockSomeServices is undefined
beforeEach(mock.inject((_$http_, $injector) => {
$http = _$http_;
mockSomeServices = $injector.get('SomeServices');
}));
//This also don't work mockSomeServices is undefined
beforeEach(mock.inject((_$http_, $injector, SomeServices) => {
$http = _$http_;
mockSomeServices = SomeServices;
}));
});
如果你想模拟一个服务,你这样做:
angular.mock.module(($provide: ng.auto.IProvideService): void => {
$provide.constant("SomeService", SomeService = { });
});
在你的控制器中,我认为你不想模拟你的服务。你想知道它被调用了,你可以通过在你的服务上放置一个间谍来做到这一点。
spyOn(SomeService, "someAction");
如果你想让它return一些数据到控制器中use/validate,你可以这样做:
spyOn(SomeService, "someAction").and.returnValue(/* data */);
然后您断言它已被调用并且数据是您所期望的:
expect(SomeService.someAction).toHaveBeenCalled();
expect(yourController.someValue).toEqual("someValue");