Node.js - Immer "Fehler: Verbindung ECONNREFUSED" beim testen-express-app mit Mokka

Zunächst, ich bin nicht lauscht auf port 80 oder 8080. Ich lausche auf port 1337.

Habe ich eine einfache HTTP-server mit express. Hier ist die app.js script um den server zu starten.

require('./lib/server').listen(1337)

Server-Skript befindet sich in lib/server.js - Datei, da das gleiche Skript auch verwendet werden, in dem test-script.

var http = require('http')
,   express = require('express')
,   app = express()

app.get('/john', function(req, res){
    res.send('Hello John!')
})

app.get('/sam', function(req, res){
    res.send('Hello Sam!')
})

module.exports = http.createServer(app)

Und schließlich die test/test.js:

var server = require('../lib/server')
,   http = require('http')
,   assert = require('assert')

describe('Hello world', function(){

    before(function(done){
        server.listen(1337).on('listening', done)
    })

    after(function(){
        server.close()
    })

    it('Status code of /john should be 200', function(done){
        http.get('/john', function(res){
            assert(200, res.statusCode)
            done()
        })
    })

    it('Status code of /sam should be 200', function(done){
        http.get('/sam', function(res){
            assert(200, res.statusCode)
            done()
        })
    })

    it('/xxx should not exists', function(done){
        http.get('/xxx', function(res){
            assert(404, res.statusCode)
            done()
        })
    })

})

Aber ich bekomme 3/3 Fehler:

Hello world
1) Status code of /john should be 200
2) Status code of /sam should be 200
3) /xxx should not exists


 3 of 3 tests failed:

1) Hello world Status code of /john should be 200:
 Error: connect ECONNREFUSED
  at errnoException (net.js:770:11)
  at Object.afterConnect [as oncomplete] (net.js:761:19)

2) Hello world Status code of /sam should be 200:
 Error: connect ECONNREFUSED
  at errnoException (net.js:770:11)
  at Object.afterConnect [as oncomplete] (net.js:761:19)

3) Hello world /xxx should not exists:
 Error: connect ECONNREFUSED
  at errnoException (net.js:770:11)
  at Object.afterConnect [as oncomplete] (net.js:761:19)

Ich verstehe wirklich nicht, warum dies, da mein test-Skript scheint Logik. Ich lief der server node app.js getestet und manuell localhost:1337/john und localhost:1337/sam und es funktioniert Super!

Hilfe? Danke.

  • Haben Sie versucht, die port-Nummer ändern?
  • Das problem bleibt das gleiche, egal, was ist der port (geändert nach dem Zufallsprinzip mehrmals).
  • Haben Sie versucht, die http://localhost:1337/john in Ihrem http.get()?
  • Dank Amberlamps, geben Sie den vollständigen Pfad funktioniert gut für alle tests. Können Sie es hinzufügen, wie Sie eine Antwort, um zu bestätigen und schließen Sie die Frage?
InformationsquelleAutor htaidirt | 2013-03-07
Schreibe einen Kommentar