2017-08-14 76 views
1

我正在使用Express.js应用程序。目前的功能是创建与发布请求的约会,并从第三方API获取和保存数据,然后在后续请求中发送更新的API数据。该功能完全正常,但在测试中,获取API数据的功能没有被调用。摩卡测试中没有调用函数

路线创建约会:

app.post('/schedule', requestHandler.postSchedule); 

创建约会请求处理程序:

requestHandler.postSchedule = function (req, res) { 
    let appointment = { 
    // posted data 
    }; 

    new Appointment(appointment) 
    .save() 
    .then(newAppointment => { 
     if(req.body.cityName && req.body.cityName !== '') { 
     console.log('req.body.cityName', req.body.cityName); 
     weatherHelper.addNewCityWeatherData(req.body.cityName); 
     } 
     return newAppointment; 
    }) 
    .then(newAppointment => { 
     // do some other stuff 
     res.send(newAppointment); 
    }) 
    .catch(err => { 
     error(err); 
    }); 
}; 

功能添加天气数据:

exports.addNewCityWeatherData = (city) => { 
    console.log('City in addNewCityWeatherData', city); 
    getCurrentTrackingCities(cities => { 
    if(cities.indexOf(city) < 0) { 
     console.log(city + ' data not in weather'); 
     getWeatherData(city, data => { 
     console.log('Got weather data'); 
     addWeatherDataToDB(city, data); 
     }); 
    } else { 
     console.log('City exists'); 
    } 
    }); 
}; 

函数来获取从API气象数据:

getWeatherData = (city, callback) => { 
    console.log('getWeatherData called', city); 
    let url = `http://api.apixu.com/v1/forecast.json?key=${weatherApiKey}&q=${city}&days=${10}` 
    request(url, (err, res, body) => { 
    console.log('Weather data received body'); 
    callback(body); 
    }); 
}; 

测试时,此功能失败,除了'天气数据收到正文'以及所有功能的日志外,所有控制台日志都会打印。

这里是我的测试:

describe.only('Weather data', function() { 
    let requestWithSession = request.defaults({jar: true}); 

    let hashedPass = bcrypt.hashSync('testpass', null); 

    beforeEach((done) => { 
    new User({ 
     'name': 'Test User', 
     'email': '[email protected]', 
     'password': hashedPass 
    }) 
    .save() 
    .then(() => { 
     let options = { 
     'method': 'POST', 
     'uri': testHost + '/login', 
     'form': { 
      'email': '[email protected]', 
      'password': 'testpass' 
     } 
     }; 
     requestWithSession(options, (err, res, body) => { 
     done(); 
     }); 
    }); 
    }); // beforeEach 

    afterEach((done) => { 
    // remove test stuff from db 
    }); // afterEach 

    it('Adds weather data when an appointment with new city is posted', (done) => { 
    let options = { 
     'method': 'POST', 
     'uri': testHost + '/schedule', 
     'form': { 
     'title': 'Test title', 
     'description': 'Test description', 
     'start_date_time': '2017-07-19 01:00', 
     'end_date_time': '2017-07-19 02:00', 
     'cityName': 'New York', 
     'isTrackingWeather': 'true' 
     } 
    }; 

    // post request to add appointment data 
    requestWithSession(options, (err, res, body) => { 
     if(err) { 
     console.log('DatabaseError in Weather Data'); 
     throw { 
      type: 'DatabaseError', 
      message: 'Failed to create test setup data' 
     }; 
     } 

     let options = { 
     'method': 'GET', 
     'uri': testHost + '/allweather' 
     }; 

     // subsequesnt request to get updated weather data 
     requestWithSession(options, (err, res, body) => { 
     let found = false; 
     weatherData = JSON.parse(body); 
     // console.log('weatherData in test', weatherData); 
     weatherData.forEach(weather => { 
      if(weather.location && weather.location.name === 'New York') { 
      found = true; 
      } 
     }); 
     expect(found).to.be.true; 
     done(); 
     }); 

    }); 
    }); 
}); // Weather Data 

这里是终端输出:

Mocha test

谁能请告诉我,我究竟做错了什么?

回答

1

运行测试时,测试套件向测试服务器发出请求,处理测试服务器中的请求的代码向另一主机发送另一个请求。

您不会看到'Weather data received body',因为您的测试服务器处理的请求不是,而是等待用于请求测试服务器本身。 addNewCityWeatherData没有回调,也没有返回一个承诺,所以调用它的代码继续其快乐的方式,而不用等待它完成。您应该修改它以允许调用代码等待结果。

此外,我没有看到来自您的测试服务器发起的请求的数据如何被折回到来自测试套件的请求中。您可能还需要为此添加一些代码,除非addWeatherDataToDB(city, data);以某种方式自动处理它。