2017-12-14 5 views
0

私のユニットテストに合格できず、間違った種類のデータを使って投稿していると思います。マイルート:あなたは私のルートを見ることができるようにChai(Node.js)によるポストメソッド

let upload = multer({ 
    storage: storage 
}); 

router.route('/') 
    .post(upload.any('image'), function (req, res, next) { 
     let memory = new Memory(); 
     if (req.files.length === 0) { 
      Object.assign(memory, req.body); 
     } else { 
      Object.assign(memory, req.body, {'image': req.file.secure_url}); 
     } 

     memory.save(function (err) { 
      if (err) { 
       return res.send(err); 
      } 
      res.json({message: 'Memory Created', memory}); 
     }); 
    }) 

は、入力としてform-data受け付けmulter使用しています。しかし、私のChai testに:

it('it should not post an item without a location field', (done) => { 
     let formData = new FormData(); 
     formData.append('description', "First time we met"); 
     formData.append('image','n/a'); 

     chai.request(server) 
      .post('/api/memory') 
      .set('Accept', 'application/form-data') 
      .send(formData) 
      .end((err, res) => { 
       res.should.have.status(200); 
       res.body.should.be.a('object'); 
       res.body.should.have.property('errors'); 
       res.body.errors.should.have.property('location'); 
       res.body.errors.location.should.have.property('kind').eql('required'); 
       done(); 
      }); 

私はチャイのsendメソッドを使用していますが、このテストは単にフリーズし、私に何の応答を与えません。だから私はpostmanを使ってみましたが、x-www-form-urlencodedを使ってデータを送った場合、それはうまくいきますが、form-dataを使ってデータを送るとうまくいきます。だから私はチャイを使ってx-www-form-urlencdedにデータを送っていると思う。これをどうやって解決するのですか? (注:私は.set('Accept', 'application/form-data')を使用してみました)

答えて

0

単に.field()

describe('/POST item',() => { 
    it('it should not post an item without a location field', (done) => { 
     chai.request(server) 
      .post('/api/memory') 
      .set('Accept', 'application/form-data') 
      .field('description', "First time we met") 
      .end((err, res) => { 
       res.should.have.status(200); 
       res.body.should.be.a('object'); 
       res.body.should.have.property('errors'); 
       res.body.errors.should.have.property('location'); 
       res.body.errors.location.should.have.property('kind').eql('required'); 
       done(); 
      }); 
    }); 
を使用
関連する問題