2016-10-31 65 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