从誓言启动服务器进行测试的正确方法是什么?
我有一个快速服务器,我正在使用誓言进行测试。我想从誓言测试套件中运行服务器,这样我就不需要让它在后台运行以使测试套件正常工作,然后我可以创建一个运行服务器并在其中测试它的蛋糕任务隔离。
在 server.coffee
中,我创建了(快速)服务器,对其进行配置,设置路由并调用 app.listen(port),如下所示:
# Express - setup
express = require 'express'
app = module.exports = express.createServer()
# Express - configure and set up routes
app.configure ->
app.set 'views', etc....
....
# Express - start
app.listen 3030
在我的简单 routes-test.js 我有:
vows = require('vows'),
assert = require('assert'),
server = require('../app/server/server');
// Create a Test Suite
vows.describe('routes').addBatch({
'GET /' : respondsWith(200),
'GET /401' : respondsWith(401),
'GET /403' : respondsWith(403),
'GET /404' : respondsWith(404),
'GET /500' : respondsWith(500),
'GET /501' : respondsWith(501)
}).export(module);
其中 respondsWith(code)
的功能与誓言文档中的功能相似...
当我在上述测试中 require
服务器时,它会自动开始运行服务器并且测试运行并通过,这是很好,但我觉得我没有以“正确”的方式做这件事。
我对服务器何时启动没有太多控制权,如果我想将服务器配置为指向“测试”环境而不是默认环境,或者更改测试时的默认日志记录级别,会发生什么情况?
PS 我打算将我的誓言转换为 Coffeescript,但现在一切都在 js 中,因为我正在从文档中学习模式!
I have an express server which I am testing using vows. I want to run the server from within the vows test suite, so that I dont need to have it running in the background in order for the test suite to work, then I can just create a cake task which runs the server and tests it in isolation.
In server.coffee
I have created the (express) server, configured it, set up routes and called app.listen(port) like this:
# Express - setup
express = require 'express'
app = module.exports = express.createServer()
# Express - configure and set up routes
app.configure ->
app.set 'views', etc....
....
# Express - start
app.listen 3030
In my simple routes-test.js
I have :
vows = require('vows'),
assert = require('assert'),
server = require('../app/server/server');
// Create a Test Suite
vows.describe('routes').addBatch({
'GET /' : respondsWith(200),
'GET /401' : respondsWith(401),
'GET /403' : respondsWith(403),
'GET /404' : respondsWith(404),
'GET /500' : respondsWith(500),
'GET /501' : respondsWith(501)
}).export(module);
where respondsWith(code)
is similar in functionality to the one in the vows doc...
When I require
server in the above test, it automatically begins running the server and the tests run and pass, which is great, but I dont feel like I am doing it the 'right' way.
I dont have much control over when the server begins, and what happens if I want to configure the server to point to a 'test' environment rather than the default one, or change the default logging level for when im testing?
PS I am going to convert my vows to Coffeescript but for now its all in js as im in learning mode from the docs!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个有趣的问题,因为昨晚我做了你想做的事。我有一个小小的 CoffeScript Node.js 应用程序,它的编写方式恰好与您展示的一样。然后,我重构了它,创建了以下
app.coffee
:现在我有一个
index.coffee
(相当于您的server.coffee
):简单如下:然后,我使用 Jasmine-node 和 Zombie.js。测试框架不同,但原理是相同的:
要点是:我所做的和我建议的是在启动服务器的模块中创建一个函数。然后,在任何你想要的地方调用这个函数。我不知道这是否是“好的设计”,但它有效并且对我来说似乎具有可读性和实用性。
另外,我怀疑 Node.js 和 CoffeScript 中还没有“好的设计”。这些都是全新的、非常创新的技术。当然,我们可以“感觉有些不对劲”——就像这种情况,两个不同的人不喜欢这个设计并改变了它。我们可以感受到“错误的方式”,但这并不意味着已经有“正确的方式”。总而言之,我相信我们必须在你的发展中发明一些“正确的方法”:)
(但是询问好的做事方法也是很好的。也许有人有一个好主意,公开讨论会对你有所帮助。其他开发商。)
That is an interesting question because exactly last night I did what you want to do. I have a little CoffeScript Node.js app which happened to be written like the one you showed. Then, I refactored it, creating the following
app.coffee
:Now I have an
index.coffee
(equivalent to yourserver.coffee
) as simple as:Then, I wrote some tests using Jasmine-node and Zombie.js. The test framework is different but the principle is the same:
The point is: what I did and I would suggest is to create a function in a module which starts the server. Then, call this function wherever you want. I do not know if it is "good design", but it works and seems readable and practical to me.
Also, I suspect there is no "good design" in Node.js and CoffeScript yet. Those are brand new, very innovative technologies. Of course, we can "feel something is wrong" - like this situation, where two different people didn't like the design and changed it. We can feel the "wrong way", but it does not mean there is a "right way" already. Summing up, I believe we will have to invent some "right ways" in your development :)
(But it is good to ask about good ways of doing things, too. Maybe someone has a good idea and the public discussion will be helpful for other developers.)