Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing 角度2测试-process.env_Testing_Angular_Mocking_Testbed - Fatal编程技术网

Testing 角度2测试-process.env

Testing 角度2测试-process.env,testing,angular,mocking,testbed,Testing,Angular,Mocking,Testbed,我试图在我的应用程序中模拟请求,但流程变量存在问题。我将url存储在process.env.backendUrl中,指向后端API。在RestService中,我有: constructor(private http: Http) { this.rest = process.env.backendUrl + "/api/"; } 现在不可能运行测试,因为在例如LoginComponent中,我有RestService依赖项,并且出现以下错误: zone.js:140 Uncaught

我试图在我的应用程序中模拟请求,但流程变量存在问题。我将url存储在process.env.backendUrl中,指向后端API。在RestService中,我有:

constructor(private http: Http) {
    this.rest = process.env.backendUrl + "/api/";
}
现在不可能运行测试,因为在例如LoginComponent中,我有RestService依赖项,并且出现以下错误:

zone.js:140 Uncaught Error: Error in ./LoginComponent class LoginComponent_Host - inline template:0:0 caused by: process is not defined
ReferenceError: process is not defined
at new RestService (http://localhost:9876/base/src/test.ts:21595:2790)
at DynamicTestModuleInjector.get (DynamicTestModule.ngfactory.js:170:67)
at DynamicTestModuleInjector.get (DynamicTestModule.ngfactory.js:180:93)
at DynamicTestModuleInjector.getInternal (DynamicTestModule.ngfactory.js:255:51)
at DynamicTestModuleInjector.NgModuleInjector.get (http://localhost:9876/base/src/test.ts:25036:27)
at TestBed.get (http://localhost:9876/base/src/test.ts:5589:51)
at _View_LoginComponent_Host0.createInternal (LoginComponent_Host.ngfactory.js:16:74)
at _View_LoginComponent_Host0.AppView.create (http://localhost:9876/base/src/test.ts:36192:21)
at _View_LoginComponent_Host0.DebugAppView.create (http://localhost:9876/base/src/test.ts:36404:44)
我在environment.ts(由cli创建的文件)中设置了process.env.backendUrl


我应该在其他地方设置它,还是有什么方法可以告诉karma这个变量?

如果您使用angular cli,您应该将
后端URL
添加到
环境中

export const environment = {
  production: false,
  backendUrl: 'http://localhost:8080'
};
您还应该将其添加到
环境.prod.ts
文件中,将url设置为生产url。在生产环境中构建时,将使用
.prod
文件

在您的文件中,您应该导入
环境
(从
环境.ts
文件导入),只需使用
环境.backendUrl

另请参见:

export const environment = {
  production: false,
  backendUrl: 'http://localhost:8080'
};