簡體   English   中英

從 vows 啟動服務器進行測試的正確方法是什么?

[英]What is the correct way to launch your server from vows for testing?

我有一個快遞服務器,我正在使用 vows 進行測試。 我想從 vows 測試套件中運行服務器,這樣我就不需要讓它在后台運行以使測試套件工作,然后我可以創建一個運行服務器並對其進行測試的蛋糕任務隔離。

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)在功能上與 vows 文檔中的類似...

當我在上述測試中require服務器時,它會自動開始運行服務器並且測試運行並通過,這很好,但我覺得我做的不是“正確”的方式。

我對服務器何時啟動沒有太多控制權,如果我想將服務器配置為指向“測試”環境而不是默認環境,或者在我測試時更改默認日志記錄級別,會發生什么?

PS我要將我的誓言轉換為 Coffeescript 但現在它全部在 js 中,因為我在文檔中處於學習模式!

這是一個有趣的問題,因為昨晚我做了你想做的事。 我有一個小的 CoffeScript Node.js 應用程序,它恰好像你展示的那樣編寫。 然后,我重構了它,創建了以下app.coffee

# ... Imports
app = express.createServer()

# Create a helper function
exports.start = (options={port:3000, logfile:undefined})->
  # A function defined in another module which configures the app
  conf.configure app, options 
  app.get '/', index.get
  # ... Other routes
  console.log 'Starting...'
  app.listen options.port

現在我有一個index.coffee (相當於您的server.coffee ),如下所示:

require('./app').start port:3000

然后,我使用Jasmine-nodeZombie.js編寫了一些測試。 測試框架不同,但原理是一樣的:

app = require('../../app')
# ...

# To avoid annoying logging during tests
logfile = require('fs').createWriteStream 'extravagant-zombie.log'

# Use the helper function to start the app
app.start port: 3000, logfile: logfile

describe "GET '/'", ->
  it "should have no blog if no one was registered", ->
    zombie.visit 'http://localhost:3000', (err, browser, status) ->
      expect(browser.text 'title').toEqual 'My Title'
      asyncSpecDone()
    asyncSpecWait()

關鍵是:我所做的並且我建議的是在啟動服務器的模塊中創建一個 function。 然后,在任何你想要的地方調用這個 function。 我不知道它是否是“好的設計”,但它對我來說是可行的,並且看起來可讀且實用。

另外,我懷疑 Node.js 和 CoffeScript 中還沒有“好的設計”。 這些是全新的、非常創新的技術。 當然,我們可以“感覺有問題”——比如這種情況,兩個不同的人不喜歡設計並改變了它。 我們可以感覺到“錯誤的方式”,但這並不意味着已經有“正確的方式”。 總結一下,我相信我們將不得不在您的開發中發明一些“正確的方法”:)

(但問一些好的做事方式也很好。也許有人有一個好主意,公開討論會對其他開發人員有所幫助。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM