Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
Mocha and Chai unit testing AssertionError

I don't understand why these are failing the variations I have tried are:

response.body.should.be.a('object').and.to.have.property('id');

and

response.body.should.have.property('id');

I get the error Uncaught AssertionError: expected { Object (starss) } to have property 'id'

here is my JSON payload from 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"
    }
}

here are my Mocha tests

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 Respostas
Responde à pergunta

0

The reason why it's not finding the id property is because the response body only has the key of starss. You need to go into the starss object to access its keys. I used to mocha/chai codepen to verify the result:

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

The URL for the codepen is here: https://codepen.io/alexpyzhianov/pen/KVbeyO

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda