0

その日があなたを見つけることを願っています。

そのため、Node でいくつかの TDD チョップを構築しようとしています。そのために、単純な GET および POST リクエストを実行する超ベアボーン アプリを構築しました。世界で最も単純なフォームを提供し、ユーザーがこのフォームに入力した内容を画面に表示するだけです。これは未使用のノードであり、フレームワークは含まれていません。テストに Mocha と Superagent を使用していますが、POST テストでスタックしています。これが私のアプリです:

    var http = require('http');
    var qs = require('querystring');

    var server = http.createServer(function(req, res){
        switch(req.method){
            case 'GET':
                console.log("Calling get");
                res.statusCode = 200;
                res.setHeader('Content-Type', 'text/html');
                res.end("<p>Hello World!</p>" + 
                    "<form method='post' action='/'>" +
                    "<input type='text' name='field'>" + 
                    "<input type='submit'>" +
                    "</form>");
                break;
            case 'POST':
                var body = "";
                req.on('data', function(data){
                    body += data;
                })
                req.on('end', function(){
                    var post = qs.parse(body);
                    res.statusCode = 200;
                    res.setHeader('Content-Type', 'text/html');
                    res.end("<p>" + post.field + "</p>")
                    // console.log(req)
                    console.log(post);
                });
        }
    })
    server.listen(8080);

    console.log('Server running on port 8080.')

そして、ここに私のテストがあります:

    var request = require('superagent');
    var expect = require('expect.js');

    describe('Main page', function(){
        it("should get 'Hello World!'", function(done){
            request.get('localhost:8080').end(function(res){
                expect(res).to.exist;
                expect(res.status).to.equal(200);
                expect(res.text).to.contain("World");
                done();
            });
        });

        it("should display input text from a form.", function(done){
            request.post('localhost:8080')
                .send({field: "Test string."})
                .end(function(res){
                    expect(res).to.exist;
                    expect(res.status).to.equal(200);
                    expect(res.text).to.contain("Test");
                    done();
            })
        });
    });

物事を学ぶときは、自分がしていることを分離できるように、できるだけシンプルに保つのが好きです. Superagent について私が知っていることから、.send() メソッドは、さまざまなポスト キーと値を含むオブジェクトを取得する必要があります。これはアプリに渡され、指定されたルートに沿って実行されます。しかし、テストを実行すると、expect(res.text).to.contain("Test") アサーション以外はすべてパスします。Mocha が予期したエラーが表示されます。

未定義

' 'Test' を含むようにします。アプリを起動してブラウザで実行すると、すべて問題ありません。

私はしばらくこれと格闘してきましたが、今はハイブマインドに行きます。前述したように、私は TDD の初心者ですが、テストの神様になりたいと思っています。どんな啓発も大歓迎です。

4

1 に答える 1

2

そして自力で手に入れました。十分に長い間見つめていると、ドキュメントが教えてくれる驚くべきこと。

ここにあるドキュメントを見た後:

http://visionmedia.github.io/superagent/#post-/%20put%20requests

Superagent が適切に投稿するには、情報が送信される前に投稿の種類を次のように伝える必要があることに気付きました。

    it("should display input text from a form.", function(done){
        request.post('localhost:8080')
            .type('form')
            .send({field: "Test string."})
            .end(function(res){
                expect(res).to.exist;
                expect(res.status).to.equal(200);
                expect(res.text).to.contain("Test");
                done();
        })
    });

気の利いたもの。他の人がこれが役立つことを願っています。

于 2014-09-08T01:53:56.157 に答える