【问题标题】:How to force a function to execute first in Jasmine test?如何强制在 Jasmine 测试中首先执行函数?
【发布时间】:2016-05-25 04:11:45
【问题描述】:

我在 Node 中有一条获取身份验证密钥的路由。我想在我的所有 jasmine 测试中使用此身份验证密钥作为 URL 请求中的参数。我希望 SetUp 函数运行,设置一个全局变量,然后允许我在所有其余的测试用例中使用这个变量。

设置功能

    var global_key = request({
      uri               : 'http://localhost:3000/grabToken',
      method            : 'GET'
    },
    function (err, body, res) {
      if (err) { console.log(err);}
      else {
        return body['auth_key'];
      }
    });

测试套件

function testCases() {
  describe(TEST_SUITE, function() {
    describe("GET /retrieveSecret/VALID_UUID", function() {
      it('Requests the secret - Successful response', function(done) {
        // ...
      }
    }
  }
}

【问题讨论】:

    标签: javascript node.js jasmine


    【解决方案1】:

    您可以使用asynchronous 版本的beforeAll 函数:

    describe(TEST_SUITE, function() {
        let key;
    
        beforeAll(function (done) {
            const params = { uri: 'http://localhost:3000/grabToken', method: 'GET' };
    
            request(params, function (err, body, res) {
                if (err) {
                    console.log(err);
                    done.fail();
                } else {
                    key = body['auth_key'];
                    done();
                }
            });
        })
    
        describe("GET /retrieveSecret/VALID_UUID", function() {
            it('Requests the secret - Successful response', function(done) {
                // `key` is available here
            }
        });
    })
    

    【讨论】:

      猜你喜欢
      • 2012-06-06
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多