简体   繁体   中英

Karma with Jasmine: expect undefined

I am working with karma & jasmine for unit testing my javascript pages. After all configuration done i was able to run the test cases. However, the expect statement is falining stating undefined. even though i hard code 2 strings in the expect, its failing. I tried toBe & toEqual but without success. Below is my code:

describe('Sanity Test', function() {
    var scope;
    beforeEach(angular.mock.module('serviceApp'));
    beforeEach(angular.mock.inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        $controller('welcomeController', {
            $scope : scope
        });
    }));


    it('Sanity test Jasmine"', function() {
        scope.text = 'Hi';      
        expect('Hi').toEqual('Hi');
    });
});

Error: INFO [launcher]: Starting browser Chrome INFO [Chrome 30.0.1599 (Windows 7)]: Connected on socket DjMqv6LulftBpkJ2ph7g Chrome 30.0.1599 (Windows 7) Sanity Test Sanity test Jasmine" FAILED expect undefined toEqual "Hi" .....src/main/webapp/test/spec/controllers/welcome.test.js:15:3: expected "Hi" but was undefined Chrome 30.0.1599 (Windows 7): Executed 1 of 1 (1 FAILED) ERROR (0.424 secs / 0.025 secs)

Been struggling for last 2 days.

May be something to do with the karma version i was using. I moved to mac with a slightly older version of karma and everything worked smooth.

I was getting undefined messages like this for all my tests in an Ionic project which ran with grunt. It ended up being because my karma settings were configured to use mocha and chai instead of jasmine. Just edit your configuration (whether its gulpfile.js, Gruntfile.js or karma.my.js) from

frameworks: ['mocha', 'chai'],

to

frameworks: ['jasmine'],

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