0

Jasmine AngularJS テスト (合格karma start configs/karma.conf.js)

describe('IndexController', function () {
    beforeEach(module('myApp'));

    var ctrl, scope;

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

    it('should add name parameter to scope', function () {
        expect(scope.name).toBeDefined();
    });
});

controllers.js の内容

var myApp = angular.module('myApp', []);

myApp.controller('IndexController', function ($scope) {
        $scope.name = 'bob';
});

出力:jasmine-node test/ --junitreport

   Message:
     TypeError: object is not a function
   Stacktrace:
     TypeError: object is not a function
    at null.<anonymous> (/tmp/tests/test/unit/controllerSpec.js:38:16)
    at Object.<anonymous> (/tmp/tests/test/unit/controllerSpec.js:36:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
4

2 に答える 2

0

Angular はブラウザで実行するように作られています。ノードでは実行されません。少なくとも、多くの努力なしではありません。

それを nodeに移植する試みがありましたが、そのプロジェクトは、検索エンジンの最適化のためにサーバー側でAngularページをレンダリングすることを実際に意図しています. 本当に正当な理由がない限り、Node.js で Angular アプリをテストしようとするべきではありません。

于 2014-11-20T22:42:47.650 に答える