2014-07-24 331 views
1

我是AngularJS(我是Rails开发者)的新手,我试图测试一个使用angular-ui-calendar的简单Angular控制器。日历工作,但我想至少有一些测试覆盖这里。我知道我在做一个小白错误这里的某个地方......茉莉花基本角度测试

这里的错误:

Chrome 36.0.1985 (Mac OS X 10.8.5) Controller: UserCalendarCtrl should return event sources FAILED 
    TypeError: undefined is not a function 
     at null.<anonymous> (/Users/freddyrangel/repositories/ayuinc/viva-angular/test/spec/controllers/user-calendar-controller.spec.js:24:31) 
     at Object.invoke (/Users/freddyrangel/repositories/ayuinc/viva-angular/app/bower_components/angular/angular.js:3805:17) 
     at workFn (/Users/freddyrangel/repositories/ayuinc/viva-angular/app/bower_components/angular-mocks/angular-mocks.js:2143:20) 
    Error: Declaration Location 
     at window.inject.angular.mock.inject (/Users/freddyrangel/repositories/ayuinc/viva-angular/app/bower_components/angular-mocks/angular-mocks.js:2128:25) 
     at null.<anonymous> (/Users/freddyrangel/repositories/ayuinc/viva-angular/test/spec/controllers/user-calendar-controller.spec.js:23:14) 
     at /Users/freddyrangel/repositories/ayuinc/viva-angular/test/spec/controllers/user-calendar-controller.spec.js:3:1 
    TypeError: Cannot read property 'eventSources' of undefined 
     at null.<anonymous> (/Users/freddyrangel/repositories/ayuinc/viva-angular/test/spec/controllers/user-calendar-controller.spec.js:37:21) 

用户日历controller.spec.js

'use strict'; 

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

    var controller, 
    mockScope; 

    beforeEach(
    module('vivaAngularApp', 'ui.calendar') 
); 

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

    it('should return event sources', function() { 
    expect(mockScope.eventSources).toBe(true); 
    }); 
}); 

用户日历控制器的.js

'use strict'; 

app.controller('UserCalendarCtrl', function($scope) { 
    var date = new Date(), 
    d = date.getDate(), 
    m = date.getMonth(), 
    y = date.getFullYear(); 

    $scope.uiConfig = { 
    calendar:{ 
     height: 450, 
     editable: true, 
     header:{ 
     left: 'month basicWeek basicDay agendaWeek agendaDay', 
     center: 'title', 
     right: 'today prev,next' 
     }, 
     dayClick: $scope.alertEventOnClick, 
     eventDrop: $scope.alertOnDrop, 
     eventResize: $scope.alertOnResize 
    } 
    }; 

    $scope.eventSources = { 
    url: 'http://162.243.222.54/fullcalendar/new_fechas_admin.php' 
    }; 
}); 

模拟/ uicalendar.js

'use strict'; 

angular.module('ui.calendar'); 

karma.conf.js

// Karma configuration 
// http://karma-runner.github.io/0.10/config/configuration-file.html 

module.exports = function(config) { 
    config.set({ 
    // base path, that will be used to resolve files and exclude 
    basePath: '', 

    // testing framework to use (jasmine/mocha/qunit/...) 
    frameworks: ['jasmine'], 

    // list of files/patterns to load in the browser 
    files: [ 
     'app/bower_components/jquery/dist/jquery.js', 
     'app/bower_components/angular/angular.js', 
     'app/bower_components/moment/moment.js', 
     'app/bower_components/fullcalendar/dist/fullcalendar.min.js', 
     'app/bower_components/angular-mocks/angular-mocks.js', 
     'app/bower_components/angular-resource/angular-resource.js', 
     'app/bower_components/angular-cookies/angular-cookies.js', 
     'app/bower_components/angular-sanitize/angular-sanitize.js', 
     'app/bower_components/angular-route/angular-route.js', 
     'app/bower_components/angular-ui-calendar/src/calendar.js', 
     'app/scripts/*.js', 
     'app/scripts/**/*.js', 
     'test/mock/**/*.js', 
     'test/spec/**/*.js' 
    ], 

    // list of files/patterns to exclude 
    exclude: [], 

    // web server port 
    port: 8080, 

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


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // Start these browsers, currently available: 
    // - Chrome 
    // - ChromeCanary 
    // - Firefox 
    // - Opera 
    // - Safari (only Mac) 
    // - PhantomJS 
    // - IE (only Windows) 
    browsers: ['Chrome'], 


    // Continuous Integration mode 
    // if true, it capture browsers, run tests and exit 
    singleRun: false, 

    plugins : [ 
     'karma-chrome-launcher', 
     'karma-jasmine' 
    ] 
    }); 
}; 

回答

1

这里是我所看到的,到目前为止:

  • mockScope = $rootScope.$new();,注意到$new()
  • mock/uicalendar.js,如果你想覆盖该模块,应该是angular.module('ui.calendar', []);
+0

这是完全的老兄!谢谢! – freddyrangel