angular mock `module` resulting in '[Object object] is not a function'

徘徊边缘 提交于 2019-12-23 06:47:07

问题


I'm trying to create some unit tests in Angular using Jasmine being run through Teaspoon. The tests are running, however I have a simple test just to test the existence of a controller that is failing. I have the following test setup.

//= require spec_helper

require("angular");
require("angular-mocks");
var app = require("./app");


describe("My App", function() {

  describe("App Controllers", function() {

    beforeEach(module("app"))

    it("Should have created an application controller", inject(function($rootScope, $controller){
      var scope = $rootScope.$new();
      ctrl = $controller("ApplicationCtrl", { $scope: scope });
    }));

  })

})

The require statements are processed by Browserify which is handling my dependencies, but I can also hook into sprockets which I'm using for the spec helper.

Inside the app that is being required, I have

require("angular");
var controllers = require("./controllers");

var app = angular.module("app", [
  "app.controllers"
]);

exports.app = app;

When I run this test, I get the following error produced

Failure/Error: TypeError: '[object Object]' is not a function (evaluating 'module("aialerts")')

I've spent quite a while trying to figure this out but I have no idea what's going on. Any help appreciated.


回答1:


I had the same problem. Change this line:

beforeEach(module("app"))

to:

beforeEach(angular.mock.module("app"))



回答2:


Browserify uses Node-style require, where module is an object that you can use to export functionality:

console.log(module); // {exports: {}}

angular-mocks.js tries to attach a function to window.module, but that's not possible in Browserify/Node.

Taking a look through the angular-mocks source, it appears that angular-mocks also attaches the module function to angular.mock. So instead of using the global module object, you must use angular.mock.module.



来源:https://stackoverflow.com/questions/20968320/angular-mock-module-resulting-in-object-object-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!