2015-07-02 19 views
17

Câu hỏi: Làm cách nào để viết bài kiểm tra yêu cầu bài viết trong mocha để kiểm tra xem câu trả lời có phù hợp không?Làm thế nào để viết một bài kiểm tra yêu cầu bài viết trong mocha với dữ liệu để kiểm tra nếu đáp ứng phù hợp?

Phản hồi sẽ chỉ là chuỗi url vì đó là chuyển hướng cho dịch vụ của bên thứ ba.

làm việc Ví dụ Payload:

curl -H "Content-Type: application/json" -X POST -d '{"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}}' http://localhost:9000/api/members 

member.controller.js // bài phương pháp

// Creates a new member in the DB. 
exports.create = function(req, res) { 
    Member.findByIdAndUpdate(req.body.participant.nuid, 
    { "$setOnInsert": { "_id": req.body.participant.nuid } }, 
     { "upsert": true }, 
     function(err,doc) { 
     if (err) throw err; 
     res.send({ 
      'redirectUrl': req.protocol + '://' + req.get('host') + '/registration/' + req.body.participant.nuid 
     }) 
    } 
); 
}; 

Dự kiến ​​res.send

{"redirectUrl":"http://localhost:9000/registration/98ASDF988SDF89SDF89989SDF9898"} 

Làm việc Ví dụ GET yêu cầu thử nghiệm

var should = require('should'); 
var app = require('../../app'); 
var request = require('supertest'); 

describe('GET /api/members', function() { 

    it('should respond with JSON array', function(done) { 
    request(app) 
     .get('/api/members') 
     .expect(200) 
     .expect('Content-Type', /json/) 
     .end(function(err, res) { 
     if (err) return done(err); 
     res.body.should.be.instanceof(Array); 
     done(); 
     }); 
    }); 
    it('should respond with redirect on post', function(done) { 
    // need help here 
    }); 
}); 

Trả lời

10

Hãy thử với điều này:

it('should respond with redirect on post', function(done) { 
    request(app) 
     .post('/api/members') 
     .send({"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}}) 
     .expect(200) 
     .expect('Content-Type', /json/) 
     .end(function(err, res) { 
     if (err) done(err); 
     res.body.should.have.property('participant'); 
     res.body.participant.should.have.property('nuid', '98ASDF988SDF89SDF89989SDF9898'); 
     done(); 
     }); 
    }); 
+2

Chỉ bị lẫn lộn với superagent;) – javierfdezg

+1

điều gì có nghĩa là 'app'variable? –

+5

'LoạiError: yêu cầu (...). Post (...). Gửi không phải là một hàm' –

1

Bạn cũng có thể đặt loại thành "hình thức" và gõ nội dung vào JSON như tôi hiển thị bên dưới:

it("returns a token when user and password are valid", (done) => { 
    Users.createUserNotAdmin().then((user: any) => { 
     supertestAPI 
     .post("/login") 
     .set("Connection", "keep alive") 
     .set("Content-Type", "application/json") 
     .type("form") 
     .send({"email": user.email, password: "123456"}) 
     .end((error: any, resp: any) => { 
      chai.expect(JSON.parse(resp.text)["token"].length).above(400, "The token length should be bigger than 400 characters."); 
      done(); 
     }) 
    }); 
}); 

Bạn cũng phải đặt trình phân tích cú pháp nội dung khi bạn tạo máy chủ như tôi hiển thị bên dưới:

server.use(bodyParser.urlencoded({ extended: false })); 
server.use(bodyParser.json()); 
Các vấn đề liên quan