如何在单元测试中模拟环境文件导入 [英] How to mock environment files import in unit tests

查看:413
本文介绍了如何在单元测试中模拟环境文件导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的角度应用程序中,我们使用环境文件加载一些配置.

In our angular app, we use environment files to load some config.

environment.ts

environment.ts

export const environment = {
  production: false,
  defaultLocale: 'en_US',
};

然后我们在一项服务中使用它:

We then use it in one of our service:

import { environment } from '../../environments/environment';
import { TranslateService } from './translate.service';

@Injectable()
export class LocaleService {
  constructor(private translateService: TranslateService){}

  useDefaultLocaleAsLang(): void {
    const defaultLocale = environment.defaultLocale;
    this.translateService.setUsedLang(defaultLocale);
  }
}

所以我在服务方法中使用环境文件中的值.

So I use the values in environment file in a service method.

在我们的测试文件中,我们当然可以在translateService上进行间谍:

In our test file, we can of course Spy on the translateService:

translateService = jasmine.createSpyObj('translateService', ['setUsedLang']);

但是我不知道如何在测试文件中模拟环境值(例如,在beforeEach中).甚至出于测试目的将其转换为Subject,以便我可以对其进行更改并测试不同的行为.

But I don't know how to mock the environment values in my testing file (in a beforeEach for example). Or even transform it, for testing purpose, to a Subject so I can change it and test different behaviors.

更笼统地说,如何在测试中模拟此类导入值,以确保不使用真实值?

More generally speaking, how can you mock such imports values in tests to be sure not to use real values?

推荐答案

您无法测试/模拟environment.ts.它不是Angular DI系统的一部分,它是对文件系统上文件的硬性依赖. Angular的编译过程使您可以在进行构建时替换掉不同的environment.*.ts文件.

You can't test/mock environment.ts. It is not part of Angular's DI system, it is a hard dependency on a file on the filesystem. Angular's compilation process is what enables swapping out the different environment.*.ts files under the hood when you do a build.

Angular的DI系统是一种典型的面向对象的方法,可以使应用程序的各个部分更易于测试和配置.

Angular's DI system is a typical Object Oriented approach for making parts of your application more testable and configurable.

我的建议是充分利用DI系统,并尽量使用类似的方法

My recommendation would be to take advantage of the DI system and use something like this sparingly

import { environment } from '../../environments/environment';

代替Angular为这些依赖所做的事情,它希望您从中抽象出来.进行一项在environment.ts数据和您的应用程序段之间提供接缝的服务.

Instead do what Angular does for these dependencies it wants you to be abstracted away from. Make a service that provides a seam between the environment.ts data and your application pieces.

它不需要任何逻辑,它可以直接直接通过environment的属性(因此它本身不需要进行测试).

It doesn't need to have any logic, it could simply pass through the properties of environment directly (therefore it wouldn't need tested itself).

然后在依赖于environment.ts的服务/组件中,您可以在运行时使用该服务,并且在测试时可以对它进行模拟,从environment.ts以外的其他地方获取数据

Then in your services/components that depend on environment.ts at runtime you can use the service and at test time you can mock it, sourcing the data from somewhere other than environment.ts

这篇关于如何在单元测试中模拟环境文件导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆