2015-07-02 23 views
17

Soru: Cevabın uyuşup uyuşmadığını test eden mocha'da bir posta isteği testi nasıl yazabilirim?Yanıtla eşleşip eşleşmediğini test etmek için verilerle mocha'da bir gönderi isteği testi nasıl yazılır?

Yanıt, yalnızca bir üçüncü taraf hizmeti için bir yönlendirme olduğundan bir URL dizesi olacaktır.

Çalışma Örneği Taşıma kapasitesi:

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

member.controller.js // sonrası yöntemi

// 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 
     }) 
    } 
); 
}; 

Beklenen res.send

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

Çalışma Örnek istek Testi GET

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 
    }); 
}); 

cevap

10

bununla deneyin: Ayrıca "form" ve içerik türü json için tipini ayarlayabilirsiniz

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

Superagent ile karıştırdı;) – javierfdezg

+1

'app' değişkeni ne anlama geliyor? –

+5

'TypeError: request (...). Post (...) gönder işlevi değil. –

1

aşağıda göstermek gibidir:

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(); 
     }) 
    }); 
}); 

Ayrıca, sunucuyu oluştururken gövdeyi ayrıştırıcıyı ayarlamanız gerekir:

server.use(bodyParser.urlencoded({ extended: false })); 
server.use(bodyParser.json());