【问题标题】:Singleton class instance when running tests in parallel with jasmine与茉莉花并行运行测试时的单例类实例
【发布时间】:2022-01-23 18:20:45
【问题描述】:

我有一个使用 jasmine 用 javascript 编写的自动化测试设置,并并行执行测试。使用以下命令执行测试:

protractor-flake --max-attempts=2 -- ./config/configfile.js --suite $SUITE "$@";

在配置文件中,我们设置了所有 jasmine 报告器、所有测试套件的列表以及其他先决条件,例如提供有关根据环境打开或关闭的所有不同功能标志信息的类。功能标志是使用 api 调用获取的,该调用发生在 configfile.js 中的一个方法中,就在测试执行之前。我已经尝试创建一个单例并返回相同的实例,但要么我做错了什么,要么每个新线程/进程都会创建一个新实例,即使它是一个单例。 Bellow 是单例类的简化版本。

class FeatureFlagConfiguration {

 async getFlags() {
     if (FeatureFlagConfiguration.instance === undefined) {
         const client = new Client();
         this.flags = await client.getFlags();
         this.moreInfo = await client.getMoreInfo();
         FeatureFlagConfiguration.instance = this;
         Object.freeze(FeatureFlagConfiguration.instance);
     }
 }

 evaluateFlags() {
     ...
 }


 isFeatureFlagEnabled() {
     ...
 }
}
module.exports = new FeatureFlagConfiguration();

【问题讨论】:

    标签: javascript unit-testing jasmine protractor singleton


    【解决方案1】:

    尝试使用全局变量,如下所示:https://stackoverflow.com/a/1479341/3482730

    大概是这样的:

    class FeatureFlagConfiguration {
    
     async getFlags() {
         if (FeatureFlagConfiguration.instance === undefined) { // prob not needed
             const client = new Client();
             this.flags = await client.getFlags();
             this.moreInfo = await client.getMoreInfo();
             FeatureFlagConfiguration.instance = this;  // prob not needed
             Object.freeze(FeatureFlagConfiguration.instance); // prob not needed
         } // prob not needed
     }
    
     evaluateFlags() {
         ...
     }
    
    
     isFeatureFlagEnabled() {
         ...
     }
    }
    
    const featureFlagConfigurationInstance = new FeatureFlagConfiguration();
    
    module.exports = featureFlagConfigurationInstance;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-06
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      相关资源
      最近更新 更多