Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Karma测试角度代码的问题_Javascript_Angularjs_Gruntjs_Phantomjs_Karma Runner - Fatal编程技术网

Javascript 使用Karma测试角度代码的问题

Javascript 使用Karma测试角度代码的问题,javascript,angularjs,gruntjs,phantomjs,karma-runner,Javascript,Angularjs,Gruntjs,Phantomjs,Karma Runner,我想测试我的角度代码。我使用Node.js 0.10.28、Grunt、Karma作为测试框架,使用PhantomJS作为浏览器 我得到这个错误: PALM00545424A:Hello-World user$ grunt karma:unit Running "karma:unit" (karma) task INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/ INFO [launcher]: Start

我想测试我的角度代码。我使用Node.js 0.10.28、Grunt、Karma作为测试框架,使用PhantomJS作为浏览器

我得到这个错误:

PALM00545424A:Hello-World user$ grunt karma:unit

Running "karma:unit" (karma) task
INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket KPTCteUUYsm4XkgYuvGT with id 70458581
PhantomJS 1.9.7 (Mac OS X) ERROR
  TypeError: 'undefined' is not a function (evaluating 'module.factory')
  at /Users/user/repos/Hello-World/src/client/api.js:33


Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.
这是我的karma.conf.js:

module.exports = function(config) {
config.set({

  // base path that will be used to resolve all patterns (eg. files, exclude)
  basePath: './',


  // frameworks to use
  // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
  frameworks: ['jasmine'],


  // list of files / patterns to load in the browser
  files: [

    // Angular
    'src/bower/angular/angular.js',
    'src/bower/angular-route/angular-route.js',
    'src/bower/angular-bootstrap/ui-bootstrap.js',
    'src/bower/angular-mocks/angular-mocks.js',

    // Client source code
    'src/client/**/*.js',

    // Tests
    'src/tests/client/**/*.js'
  ],
  ...
有什么想法吗

编辑:这是测试代码。如果不包括角度相关性,测试将成功运行

src/tests/client/test.js

describe('some tests', function() {
    it('should run', function () {
    var val = "Hello World";
    expect(val).toBeTruthy();
    });
});
这是失败的客户端代码:

module.factory("$RPC", ['$http', '$q', function($http, $q) {
    return {
        call: function(m, f, d, c) {
            var res = {value: null, error: null, loaded: false, loading: true};
            $http.post("/api/"+m+"/"+f, d)
                .success(function(value) {
                    res.value = value;
                    res.loaded = true;
                    res.loading = false;
                    console.log("[success]", m, f, d, res);
                    if(typeof c === 'function') {
                        c(null, res);
                    }
                })
                .error(function(value) {
                    res.error = value;
                    res.loaded = true;
                    res.loading = false;
                    console.log("[error]", m, f, d, res);
                    if(typeof c === 'function') {
                        c(res.error, res);
                    }
                });
            return res;
        }
    };
}]);
我的问题解决了

“src/client/***.js”
不是一个好的解决方案,因为导入是按字母顺序进行的。确保先导入主模块


只需列出每个依赖项,并且只使用通配符
***
,顺序无关紧要。

让我们看看您的测试以及它测试的代码。现在我没有测试任何客户端代码。我只想先设置角度测试。错误发生的位置在您的问题中显示。原因是什么?不知道,我得看看密码。karma.conf.js,就像你发布的一样,看起来不错。我添加了完整的错误上下文,由于堆栈溢出限制,我无法发布所有内容。我在运行测试时发现错误,但没有看到测试失败的代码,我无法帮助您。使用通配符是可以的。我的建议是使用Yeoman,让它为你安排一切。当你甚至不知道背景时,为什么要投否决票?使用通配符并不总是好的。例如,如果文件
app.js
正在定义模块并且
api.js
使用它,
api.js
必须位于
app.js
之后。因此,您不能按字母顺序导入,您必须在此上下文中定义正确的顺序,并对其余的源文件使用通配符