Answer the question
In order to leave comments, you need to log in
How to test with Jasmine in conjunction with Angular + RequireJS?
How to write tests using this application structure https://habrahabr.ru/post/225931/
Using karmaJS test runner and jasmineJS
Controller
define(['../module'], function (controllers) {
'use strict';
controllers.controller('topMenuCtrl', ['$scope','$http', function ($scope, $http) {
$scope.list = [];
$scope.get = function () {
$http.get('../../../DB/options.json').success(function (response) {
$scope.options = response;
$scope.list = response;
});
};
}]);
});
define([
'angular',
'app/app',
'angularMocks'
],
function (angular, app) {
describe('app', function() {
var scope, ctrl, httpBackend;
beforeEach(module(app));
beforeEach(inject(function (_$httpBackend_, $rootScope, $controller) {
httpBackend = _$httpBackend_;
httpBackend.when('GET', '../../../DB/options.json').respond(['Hello world!']);
scope = $rootScope.$new();
ctrl = $controller('TopMenuCtrl', {
$scope: scope
});
}));
it('list', function() {
expect(scope.list).toEqual([]);
});
});
});
Answer the question
In order to leave comments, you need to log in
Figured it out myself =)
define([
'angular',
'app/app',
'app/controllers/module',
'angularMocks'
],
function (angular, app, controllers) {
describe('app', function() {
var scope, ctrl, httpBackend;
beforeEach(module('app.controllers'));
beforeEach(inject(function (_$httpBackend_, $rootScope, $controller) {
httpBackend = _$httpBackend_;
httpBackend.when('GET', '../../../DB/options.json').respond(['Hello world!']);
scope = $rootScope.$new();
ctrl = $controller('topMenuCtrl', {
$scope: scope
});
}));
it('list', function() {
expect(scope.list).toEqual([]);
});
});
});
TopMenuCtrl
, a topMenuCtrl
. Secondly, in addition to the app module, you also need to pass the controllers module. Then we turn to it like thisbeforeEach(module('app.controllers'));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question