2016-10-31 67 views
0

我正在測試Angular和Jasmine的角度指令。 嘲笑http後端工作正常,所有測試在本地都能正常工作。但是,在構建服務器,我得到:Jenkins構建服務器上的茉莉花測試錯誤與角度模擬?

Error: Unexpected request: GET app/auth/views/login.html No more request expected (line 1419) [email protected]_components/angular-mocks/angular-mocks.js:1419:90 [email protected]/vendor.js:222:54 build/vendor.js:219:263 build/vendor.js:254:21 [email protected]/vendor.js:268:347 [email protected]/vendor.js:265:425

我的測試設置:

beforeEach(angular.mock.module("app")); 

beforeEach(() => { 
    inject(function ($injector, _$compile_, _$rootScope_) { 
     // The injector unwraps the underscores (_) from around the parameter names when matching 
     $compile = _$compile_; 
     $rootScope = _$rootScope_; 
     $httpBackend = $injector.get("$httpBackend"); 
    }); 

$httpBackend.whenGET("api/langs/gb.json").respond({ "COMMON.HOME": homeName }); 
$httpBackend.whenGET("api/langs/de.json").respond({}); 

$httpBackend.whenGET("app/home/views/dashboard.html").respond(200, ""); 
$httpBackend.whenGET("app/home/views/login.html").respond(200, ""); 
$httpBackend.whenGET(/^private\/auth\?.*/).respond({}); 

directiveElem = getCompiledElement(); 
    }); 

什麼是構建服務器上的不同。我無法解釋這種行爲。

+0

你可以顯示你用來模擬'app/auth/views/login.html'文件的$ httpBackend代碼嗎? – TwitchBronBron

+0

你能否也顯示你的指令代碼? – TwitchBronBron

+0

您提供的示例中引用了'http://localhost/app/auth/views/login.html'。你如何在你的應用中包含'login.html'?你能分享包含該文件的代碼嗎? – TwitchBronBron

回答

1

在您的應用程序啓動過程中,UI路由器正試圖加載app/auth/views/login.html文件。

如果你在本地運行Jasmine測試,你已經有一個網址爲http://localhost的網站,所以http://localhost/app/auth/views/login.html的請求會返回實際的文件。當您運行生成服務器上這個測試中,構建服務器沒有被配置爲服務於http://localhost/app/auth/views/login.html URL,因此它返回一個404

下面是描述如何解決這個問題的文章:UI-router interfers with $httpbackend unit test, angular js

而且,這裏有一個關於如何處理這個問題的更詳細的github問題:https://github.com/angular-ui/ui-router/issues/212