0

I'm attempting to write a mocha test dynamically, rather than write an expect for each item in a res.body.result. I have a json response like this:

{
  "ok": true,
  "result": {
    "year": "2000",
    "makes": [
      "Acura",
      "Audi",
      "BMW"
    ]
  }
}

and instead of writing this for each of the makes:

describe('Decode VIN', function (){
    it('should return 200 response', function (done){
        api.get('/makes_year?year=2000')
            .set('Accept', 'application.json')
            .expect(200)
            .end(function(err, res) {
                expect(res.body.ok).to.equal(true)
                expect(res.body.result.year).to.equal("2000")
                expect(res.body.result.makes).to.equal("Acura")
                expect(res.body.result.makes).to.equal("Audi")
                done();
            })
    });
});

How can I make this iteratively?

edit: I tried declaring the expectedMakes inside the describe statement and out. For brevity, this is only one of the two.

var expectedMakes = [
    "Acura",
    "Audi",
    "BMW"
];

describe('Makes By Year', function (){
    it('should return 200 response, body.ok, and an array of vehicle makes', function (done){
        api.get('/makes_year?year=2000')
            .set('Accept', 'application.json')
            .expect(200)
            .end(function(err, res) {
                expect(res.body.ok).to.equal(true)
                expect(res.body.result.year).to.equal("2000")
                expect(res.body.result.makes).to.equal(expectedMakes)
                done();
            })
    });
});

I get this result:

  Uncaught AssertionError: expected [ Array(37) ] to equal [ Array(37) ]
  + expected - actual
Christina Mitchell
  • 437
  • 2
  • 8
  • 17

1 Answers1

0

The expected values are located in your test. One way could be to store all makes as expected in your test and assert

var expectedMakes = [
   'Acura',
   'Audi',
   ... 
]
expect(res.body.result.makes).toEqual(expectedMakes)

Which will assert that the lists are strictly equal with each item being in the matching location to expectedMakes

dm03514
  • 54,664
  • 18
  • 108
  • 145