Gulp测试失败

时间:2016-03-30 00:21:28

标签: angularjs unit-testing gulp jasmine bower

当我尝试进行gulp测试时,会发生这种情况

Error: spyOn could not find an object to spy upon for logout()
        at /workspace/WebServices/src/test/javascript/spec/components/auth/auth.services.spec.js:9
    TypeError: 'undefined' is not an object (evaluating '$httpBackend.verifyNoOutstandingExpectation')
        at /workspace/WebServices/src/test/javascript/spec/components/auth/auth.services.spec.js:9
PhantomJS 1.9.8 (Linux 0.0.0): Executed 53 of 53 (53 FAILED) (0 secs / 0.776 secs)
PhantomJS 1.9.8 (Linux 0.0.0): Executed 53 of 53 (53 FAILED) ERROR (1 min 8.425 secs / 0.776 secs)
[20:17:05] 'test' errored after 1.23 min

有什么想法在这里发生?

文件中的代码

'use strict';

describe('Service Tests', function () {
    beforeEach(mockI18nCalls);
    beforeEach(mockScriptsCalls);

    describe('Auth', function () {
        var $httpBackend, spiedLocalStorageService, authService, spiedAuthServerProvider;

        beforeEach(inject(function($injector, localStorageService, Auth, AuthServerProvider) {
            $httpBackend = $injector.get('$httpBackend');
            spiedLocalStorageService = localStorageService;
            authService = Auth;
            spiedAuthServerProvider = AuthServerProvider;

            $httpBackend.expectPOST(/api\/logout\?cacheBuster=\d+/).respond(200, '');
        }));
        //make sure no expectations were missed in your tests.
        //(e.g. expectGET or expectPOST)
        afterEach(function() {
            $httpBackend.verifyNoOutstandingExpectation();
            $httpBackend.verifyNoOutstandingRequest();
        });

        it('should call backend on logout then call authServerProvider.logout', function(){
            //GIVEN
            //Set spy
            spyOn(spiedAuthServerProvider, 'logout').and.callThrough();
            spyOn(spiedLocalStorageService, "clearAll").and.callThrough();

            //WHEN
            authService.logout();
            //flush the backend to "execute" the request to do the expectedGET assertion.
            $httpBackend.flush();

            //THEN
            expect(spiedAuthServerProvider.logout).toHaveBeenCalled();
            expect(spiedLocalStorageService.clearAll).toHaveBeenCalled();
        });

    });
});

在做出Wilmer建议的更改之后

spiedLocalStorageService = $injector.get(localStorageService);
authService = $injector.get(Auth);
spiedAuthServerProvider = $injector.get(AuthServerProvider);

输出

ttp%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fhome%2Far%2FDocuments%2Fworkspace%2FWebServices%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1819)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fhome%2Far%2FDocuments%2Fworkspace%2FWebServices%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1774%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fhome%2Far%2FDocuments%2Fworkspace%2FWebServices%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1801%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fhome%2Far%2FDocuments%2Fworkspace%2FWebServices%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1745%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fhome%2Far%2FDocuments%2Fworkspace%2FWebServices%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A2348
        at /home/ar/Documents/workspace/WebServices/src/main/webapp/bower_components/angular/angular.js:9
        at forEach (/home/ar/Documents/workspace/WebServices/src/main/webapp/bower_components/angular/angular.js:9)
        at loadModules (/home/ar/Documents/workspace/WebServices/src/main/webapp/bower_components/angular/angular.js:9)
        at createInjector (/home/ar/Documents/workspace/WebServices/src/main/webapp/bower_components/angular/angular.js:9)
        at workFn (/home/ar/Documents/workspace/WebServices/src/main/webapp/bower_components/angular-mocks/angular-mocks.js:9)
    Error: spyOn could not find an object to spy upon for logout()
        at /home/ar/Documents/workspace/WebServices/src/test/javascript/spec/components/auth/auth.services.spec.js:9
    TypeError: 'undefined' is not an object (evaluating '$httpBackend.verifyNoOutstandingExpectation')
        at /home/ar/Documents/workspace/WebServices/src/test/javascript/spec/components/auth/auth.services.spec.js:9
PhantomJS 1.9.8 (Linux 0.0.0): Executed 53 of 53 (53 FAILED) (0 secs / 0.866 secs)
PhantomJS 1.9.8 (Linux 0.0.0): Executed 53 of 53 (53 FAILED) ERROR (3.279 secs / 0.866 secs)
[20:20:00] 'test' errored after 7.29 s
[20:20:00] Error: 1
    at formatError (/usr/local/lib/node_modules/gulp/bin/gulp.js:169:10)
    at Gulp.<anonymous> (/usr/local/lib/node_modules/gulp/bin/gulp.js:195:15)
    at emitOne (events.js:90:13)
    at Gulp.emit (events.js:182:7)
    at Gulp.Orchestrator._emitTaskDone (/home/ar/Documents/workspace/WebServices/node_modules/orchestrator/index.js:264:8)
    at /home/ar/Documents/workspace/WebServices/node_modules/orchestrator/index.js:275:23
    at finish (/home/ar/Documents/workspace/WebServices/node_modules/orchestrator/lib/runTask.js:21:8)
    at cb (/home/ar/Documents/workspace/WebServices/node_modules/orchestrator/lib/runTask.js:29:3)
    at removeAllListeners (/home/ar/Documents/workspace/WebServices/node_modules/karma/lib/server.js:333:7)
    at Server.<anonymous> (/home/ar/Documents/workspace/WebServices/node_modules/karma/lib/server.js:344:9)
    at Server.g (events.js:273:16)
    at emitNone (events.js:85:20)
    at Server.emit (events.js:179:7)
    at emitCloseNT (net.js:1527:8)
    at _combinedTickCallback (node.js:374:13)
    at process._tickCallback (node.js:401:11)
[INFO] ----------------------------------------

1 个答案:

答案 0 :(得分:0)

您需要正确获取localStorageService,Auth和AuthServerProvider服务的实例。

<强>错误

spiedLocalStorageService = localStorageService;
authService = Auth;
spiedAuthServerProvider = AuthServerProvider;

从右

spiedLocalStorageService = $injector.get(localStorageService);
authService = $injector.get(Auth);
spiedAuthServerProvider = $injector.get(AuthServerProvider);

您使用 $ httpBackend 正确地执行了操作:

$httpBackend = $injector.get('$httpBackend');

提示

Error: spyOn could not find an object to spy upon for ...

强调对象