简体   繁体   English

使用Karma和Jasmine进行angularJs单元测试时出错

[英]Getting error on angularJs unit testing using Karma and Jasmine

i'm trying to perform a unit test with Karma and Jasmine but i get error ,, looking at my code and result can u help me fixing this issue please? 我正在尝试使用Karma和Jasmine进行单元测试,但出现错误,请查看我的代码和结果,您能帮我解决此问题吗? I'm too new to this so the solution could be too simple..Thank you. 我对此还太陌生,所以解决方案可能太简单了。谢谢。

My Karma configuration: 我的业力配置:

        // Karma configuration

            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: [
                  'bower_components/angular/angular.js',
                  'bower_components/angular-resource/angular-resource.js',
                  'bower_components/angular-ui-router/release/angular-ui-router.js',
                  'bower_components/angular-mocks/angular-mocks.js',
                  'app/scripts/*.js',
                  'test/unit/**/*.js'
                ],

                // list of files to exclude
                exclude: [
                'test/protractor.conf.js', 'test/e2e/*.js'
                ],
                    // preprocess matching files before serving them to the browser
                // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
                preprocessors: {
                },

                // test results reporter to use
                // possible values: 'dots', 'progress'
                // available reporters:  https://npmjs.org/browse/keyword/karma-reporter
                reporters: ['progress'],

                // web server port
                port: 9876,

                // enable / disable colors in the output (reporters and logs)
                colors: true,

                // level of logging
                // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
                logLevel: config.LOG_INFO,

                // enable / disable watching file and executing tests whenever any file changes
                autoWatch: true,
                    // start these browsers
                // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
                 browsers: ['Chrome','PhantomJS', 'PhantomJS_custom'],

                // you can define custom flags
                customLaunchers: {
                  'PhantomJS_custom': {
                    base: 'PhantomJS',
                    options: {
                      windowName: 'my-window',
                      settings: {
                        webSecurityEnabled: false
                      },
                    },
                    flags: ['--load-images=true'],
                    debug: true
                  }
                },

                phantomjsLauncher: {
                  // Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
                  exitOnResourceError: true
                },

                // Continuous Integration mode
                // if true, Karma captures browsers, runs the tests and exits
                singleRun: false,

                // Concurrency level
                // how many browser should be started simultaneous
                concurrency: Infinity
                })
            }

My controller: 我的控制器:

            describe('Controller: MenuController', function () {

              // load the controller's module
              beforeEach(module('confusionApp'));

              var MenuController, scope, $httpBackend;
                //  Initialize  the controller  and a   mock    scope       
                beforeEach(inject(function  ($controller, _$httpBackend_,   $rootScope, menuFactory){
                //  place   here    mocked  dependencies                        
                    $httpBackend =  _$httpBackend_;                     
                    $httpBackend.expectGET("http://localhost:3000/dishes").respond([
                    {
                  "id": 0,
                  "name": "Uthapizza",
                  "image": "images/uthapizza.png",
                  "category": "mains",
                  "label": "Hot",
                  "price": "4.99",
                  "description": "A",
                  "comments":[{}]
                  },
                  {
                  "id": 1,
                  "name": "Zucchipakoda",
                  "image": "images/zucchipakoda.png",
                  "category": "mains",
                  "label": "New",
                  "price": "4.99",
                  "description": "A",
                  "comments":[{}]
                  }
                  ]);
                    scope   =   $rootScope.$new();
                    MenuController =    $controller('MenuController',   {                       
                    $scope: scope,  menuFactory:    menuFactory });
                    $httpBackend.flush();   

                     it('should have showDetails as false', function () {

                expect(scope.showDetails).toBeFalsy();

              });

              it('should create "dishes" with 2 dishes fetched from xhr', function(){

                  expect(scope.showMenu).toBeTruthy();
                  expect(scope.dishes).toBeDefined();
                  expect(scope.dishes.length).toBe(2);

              });

              it('should have the correct data order in the dishes', function() {

                  expect(scope.dishes[0].name).toBe("Uthapizza");
                  expect(scope.dishes[1].label).toBe("New");

              });

              it('should change the tab selected based on tab clicked', function(){

                  expect(scope.tab).toEqual(1);

                  scope.select(3);

                  expect(scope.tab).toEqual(3);
                  expect(scope.filtText).toEqual('mains');

                    });
                }));

            });

This is what is on Chrome 这就是Chrome上的功能

           Karma v1.1.1 - connected
           Chrome 51.0.2704 (Windows 10 0.0.0) is idle
           PhantomJS 2.1.1 (Windows 8 0.0.0) is idle
           PhantomJS 2.1.1 (Windows 8 0.0.0) is idle
           Chrome 51.0.2704 (Windows 10 0.0.0) is idle

And this is Terminal report: 这是终端报告:

                PS D:\programming\web\angular.js\gulp\conFusion\test>      karma start karma.conf.js
            16 07 2016 17:49:04.773:WARN [karma]: No captured browser, open http://localhost:9876/
            16 07 2016 17:49:04.820:INFO [karma]: Karma v1.1.1 server started at http://localhost:9876/
            16 07 2016 17:49:04.835:INFO [launcher]: Launching browsers Chrome, PhantomJS, PhantomJS_custom with unlimited concurrency
            16 07 2016 17:49:05.007:INFO [launcher]: Starting browser Chrome
            16 07 2016 17:49:05.054:INFO [launcher]: Starting browser PhantomJS
            16 07 2016 17:49:19.550:INFO [launcher]: Starting browser PhantomJS
            16 07 2016 17:49:19.561:INFO [phantomjs.launcher]: ACTION REQUIRED:
            16 07 2016 17:49:19.562:INFO [phantomjs.launcher]:
            16 07 2016 17:49:19.564:INFO [phantomjs.launcher]:   Launch browser at
            16 07 2016 17:49:19.571:INFO [phantomjs.launcher]:   http://localhost:9000/webkit/inspector/inspector.html?page=2
            16 07 2016 17:49:19.575:INFO [phantomjs.launcher]:
            16 07 2016 17:49:19.579:INFO [phantomjs.launcher]: Waiting 15 seconds ...
            16 07 2016 17:49:21.051:INFO [Chrome 51.0.2704 (Windows 10 0.0.0)]: Connected on socket /#YCvvpIyKkfV8Zt7uAAAA with id 31119843
            16 07 2016 17:49:21.728:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#1j0xHL_FNxuWzb3TAAAB with id 96164824
            16 07 2016 17:49:36.243:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#9xpNc4LhgBzDKWEfAAAC with id 44407334
            PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.005 secs / 0 secs)
            PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.005 secs / 0 secs)
            Chrome 51.0.2704 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.039 secs / 0 secs)
            16 07 2016 17:50:18.653:INFO [Chrome 51.0.2704 (Windows 10 0.0.0)]: Connected on socket /#_CyPLIKTHNnT1d0HAAAD with id manual-8566
            PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.004 secs / 0 secs)
            PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.008 secs / 0 secs)
            Chrome 51.0.2704 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.007 secs / 0 secs)
            Chrome 51.0.2704 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.012 secs / 0 secs)

Update: i moved the tests outside inject, but it seems i got errors in my project .. but still also can see errors related to testing itself.. here is what i get : 更新:我搬到外面注入测试,但似乎我在我的项目有错误..但仍然还可以看到相关的测试本身的错误..这里是我得到:

这是屏幕截图

what about these errors , are they related to the testing code or just showing the result of my test? 有关这些错误,他们在相关的测试代码什么,或者只是显示我的测试结果?

    PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 4 of 4 (4 FAILED) ERROR (0.038 secs / 0.055 secs)
    PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 4 of 4 (4 FAILED) ERROR (0.047 secs / 0
    PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 4 of 4 (4 FAILED) ERROR (0.038 secs / 0.055 secs)
    PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 4 of 4 (4 FAILED) ERROR (0.047 secs / 0.059 secs)
    Chrome 52.0.2743 (Windows 10 0.0.0): Executed 4 of 4 (4 FAILED) ERROR (0.117 secs / 0.057 secs)

You have included your modular tests inside the inject function, so you should re-write the code as´: 您已经将模块测试包含在注入函数中,因此您应该将代码重新编写为:

beforeEach(inject(function  ($controller, _$httpBackend_,   $rootScope, menuFactory){
                //  place   here    mocked  dependencies                        
                    $httpBackend =  _$httpBackend_;                     
                    $httpBackend.expectGET("http://localhost:3000/dishes").respond([
                    {
                  "id": 0,...
},
                  {
                  "id": 1, ..

                  }]);
                    scope   =   $rootScope.$new();
                    MenuController =    $controller('MenuController',   {                       
                    $scope: scope,  menuFactory:    menuFactory });
                    $httpBackend.flush();   
  }));

and after injection include tests: 注射后包括测试:

it('should have showDetails as false', function () {

    expect(scope.showDetails).toBeFalsy();

  });

after this edit just be careful that $httpBackend is expect to GET localhost:3000 but before that if you are using ngRoute it will GET templates so you should handle that issue as well. 进行此编辑后,请注意,$ httpBackend应该会到达GET localhost:3000,但在此之前,如果您使用的是ngRoute,它将获得GET模板,因此您也应该处理该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Jasmine和Karma / AngularJS项目进行单元测试 - Unit testing using Jasmine and Karma / AngularJS project 使用业力茉莉花进行单元测试时出现控制器错误 - Getting error in controller while unit testing using karma jasmine ReferenceError:在Jasmine单元测试的Karma上未定义AngularJS - ReferenceError : AngularJS is not defined on Karma with Jasmine unit testing AngularJS + Karma + Jasmine 控制器单元测试: - AngularJS + Karma + Jasmine Controller Unit testing : Karma / Jasmine单元测试具有依赖关系的AngularJS服务 - Karma/Jasmine Unit Testing an AngularJS Service with Dependencies 收到此错误“错误:beforeEach 需要一个函数参数; 在使用 karma 和 jasmine 测试 angularjs 时收到 [object Object]” - Getting this error “Error: beforeEach expects a function argument; received [object Object]” while testing angularjs using karma and jasmine 使用angal / jasmine进行angularjs应用程序启动单元测试时发生错误 - Error occurs on starting unit test using karma/ jasmine for angularjs application 使用Karma Jasmine单元测试角功能 - Unit Testing Angular Function using Karma Jasmine 使用Karma和Jasmine对Angular服务进行单元测试 - Unit Testing an Angular Service using Karma and Jasmine 用业力和茉莉花测试AngularJS - testing AngularJS with karma and jasmine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM