Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Pruebas unitarias de Mocha y Chai AssertionError

No entiendo por qué estos están fallando, las variaciones que he probado son:

cuerpo.de.respuesta.debe.ser.un('objeto').y.tener.propiedad('id');

y

cuerpo.de.respuesta.debería.tener.propiedad('id');

Recibo el error Uncaught AssertionError: esperaba que { Object (starss) } tuviera la propiedad 'id'

aquí está mi carga JSON de Postman

 { "starss": { "id": 1, "name": "1", "discovery_name": "1", "imageFileName": "test1.jpg", "datediscovered": "2001-01-01T00:00:00.000Z", "colour": "1", "mass": "1.000", "dfe": "1.000", "galaxy_origin": "1", "star_categories_id": 1, "createdAt": "2021-12-03", "updatedAt": "2021-12-12" } }

aquí están mis pruebas de Mocha

 describe('GET/stars/:id',() => { it("it should get one star by ID", (done) => { // Uncaught AssertionError: expected { Object (starss) } to be a starss // star GET by ID const starId = 1; chai.request(server) .get("/stars/" + starId ) .end((err,response)=>{ response.should.have.status(200); response.body.should.be.a('object'); response.body.should.be.a('object').and.to.have.property('id'); response.body.should.have.property('id'); // response.body.should.have.property('name'); // response.body.should.have.property('discovery_name'); // response.body.should.have.property('imageFileName'); // response.body.should.have.property('datediscovered'); // response.body.should.have.property('colour'); // response.body.should.have.property('mass'); // response.body.should.have.property('dfe'); // response.body.should.have.property('galaxy_origin'); // response.body.should.have.property('star_categories_id'); done(); }); });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La razón por la que no encuentra la propiedad id es porque el cuerpo de la respuesta solo tiene la clave de starss. Necesitas ir al objeto de las estrellas para acceder a sus claves. Solía codepen mocha/chai para verificar el resultado:

 mocha.setup('bdd'); var expect = chai.expect; describe('GET/stars/:id',function() { var body = { "starss": { "id": 1, "name": "1", "discovery_name": "1", "imageFileName": "test1.jpg", "datediscovered": "2001-01-01T00:00:00.000Z", "colour": "1", "mass": "1.000", "dfe": "1.000", "galaxy_origin": "1", "star_categories_id": 1, "createdAt": "2021-12-03", "updatedAt": "2021-12-12" } } it("body to be object", function() { expect(body).to.be.a('object'); }) it("it should get one star by ID", function() { console.log(body) expect(body.starss).to.contain.key('id'); // response.body.should.have.property('"id"'); }) }); mocha.run();

La URL del codepen está aquí: https://codepen.io/alexpyzhianov/pen/KVbeyO

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!