0

I'm using supertest to test a set of URLs by the same rules.

var urls = [
    "https://www.example.com",
    "https://www.example2.com"
];

urls.forEach(function (url) {
    console.log('begin');
    request = request(url)
        .get('')
        .expect(200)
        .end(function (err, res) {
            // Check for something
        });
    console.log('end');
});

When there is only 1 URL in the array, it works just fine. If I add a 2nd one, however, it fails with the output:

begin
end
begin

file.js:11
request = request(json)
^
TypeError: object is not a function

My guess is that I can't have 1 instance of supertest running twice, but I can't seem to find a solution to work around this. Any help is appreciated.

4

2 回答 2

3

这是因为您的分配request = request(url)覆盖了请求功能。

var urls = [
    "https://www.example.com",
    "https://www.example2.com"];

urls.forEach(function (url) {
    console.log('begin');
    var r = request(url)
        .get('')
        .expect(200)
        .end(function (err, res) {
        // Check for something
    });
    console.log('end');
});

在第一次迭代request中是指一个全局函数,但是当request = request(url)评估语句时,值request会更改,因为request(url)在第二次迭代中返回的值request不再是您期望的函数。

于 2015-03-04T13:05:44.537 回答
0

更改以下内容

request = request(url)
    .get('')
    .expect(200)
    .end(function (err, res) {
        // Check for something
    });

requestVariable = request(url)
    .get('')
    .expect(200)
    .end(function (err, res) {
        // Check for something
    });
于 2015-03-04T13:07:48.567 回答