简体   繁体   中英

Testing AngularJS with Jasmine via Karma

I'm testing my AngularJS app with Jasmine at the moment, and it's passing specs. I tried to run the tests with Karma, and it's failing. It appears that Karma can't see the $scope being defined in the Jasmine spec, but the Jasmine runner sees it fine.

My karma.conf.js:

frameworks = ['jasmine'];
colors = true;
singleRun = true;
files = [
    JASMINE,
    JASMINE_ADAPTER,
    ANGULAR_SCENARIO,
    ANGULAR_SCENARIO_ADAPTER,
    'lib/jquery-1.9.1.min.js',
    'lib/angular.min.js',
    'lib/angular-mocks.js',
    'lib/jquery.scrollTo.js',
    'lib/jquery-ui.min.js',
    'lib/calendar.js',
    'lib/date.js',
    'src/app.js',
    'spec/*.spec.js'
];
browsers = [
    'PhantomJS',
    'Firefox'
];

My test.spec.js:

describe('Application startup', function(){
    beforeEach(module('app'));
    describe('MainCtrl', function(){
        var scope, ctrl;
        beforeEach(inject(function($rootScope, $controller){
            scope = $rootScope.$new();
            ctrl = $controller('MainCtrl', {
                $scope: scope
            });
        }));
        it("should initialize $scope.testLoad to 42", function(){
            expect(scope.testLoad).toBe(42);
        });
});
});

The output I'm getting:

expect undefined toBe 42

As Jasmine is passing this with no problems, it's obviously either Karma or my config file. What's going on?

I removed ANGULAR_SCENARIO and ANGULAR_SCENARIO_ADAPTER and it's running fine now. Found the appropriate docs. Made a bad assumption.

Thanks to @canotto90 and @Tyler Eich for help and clarification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM