Test Won't Pass: API Response using Chai-HTTP IV

Hello all. I’m having trouble with Quality Assurance and Testing with Chai “Run Functional Tests on an API Response using Chai-HTTP IV - PUT method”. The lesson won’t pass. This is my solution

      /** Repetition is the mother of learning. **/
      // Try it again. This time without help !!
      test('send {surname: "da Verrazzano"}', function(done) {
        /** place the chai-http request code here... **/
        chai.request(server)
        .put('/travellers')
        .send ({surname: 'Giovanni'})
        .end(function(err, res){
        
        /** place your tests inside the callback **/
          assert.equal(res.status, 200, 'response status should be 200');
          assert.equal(res.type, 'application/json', "Response should be json");
         
          assert.equal(res.body.name, 'Giovanni', 'res.body.name should be "Giovanni"');
          assert.equal(res.body.surname, 'da Verrazzano', 'res.body.surname should be "da Verrazzano"' );

        done();
      });
    });

This answer fails all 5 tests. I pretty much copied the code from the last lesson (which passed).

I can get 4 of the tests to pass; somebody in another post said if you add an extra }) at the end then it helps the test pass. The first test is still failing though: All tests should pass and expected 'failed' to equal 'passed'.

I don’t really understand what the error is, because it seems right. The status is 200, it’s using json, it checks for Giovanni’s name, and checks da Verrazzano is the surname. What else should I do? :thinking:

You need to .send() the name and surname.

I tested your code and got the same results. It doesn’t make sense that the test for ‘res.body.name == Giovanni’ says it’s passing, because it’s actually failing with your code.

1 Like

Ahh, thank you! I added the name on line 3 and surname on line 9. (I think that was a problem too, I sent “da Verrazzano” and “Giovanni” both as surname.)

/** Repetition is the mother of learning. **/
// Try it again. This time without help !!
test('send {name: "Giovanni", surname: "da Verrazzano"}', function(done) {
	/** place the chai-http request code here... **/
	chai.request(server)
		.put('/travellers')
		.send({
			name: 'Giovanni',
			surname: 'da Verrazzano'
		})
		.end(function(err, res) {

			/** place your tests inside the callback **/
			assert.equal(res.status, 200, 'response status should be 200');
			assert.equal(res.type, 'application/json', "Response should be json");

			assert.equal(res.body.name, 'Giovanni', 'res.body.name should be "Giovanni"');
			assert.equal(res.body.surname, 'da Verrazzano', 'res.body.surname should be "da Verrazzano"');

			done();
		});
});

A related question:
The lesson “API Response using Chai-HTTP IV” is supposed to take code from the previous lesson, API Response using Chai-HTTP III, and duplicate it. I looked back at my solution for III and I’m wondering if it has the same error:

  • In the test('send {...}') and the chai.request(server).send({...}) sections, I only send the surname (Columbus) and not the name (Cristoforo).
  • But the two assert.equal(res.body...) are testing for name, and surname.
  • I think the FCC test passed my solution in error. It needs to .send() the name as well?
1 Like

Hey Adam,

I’m sorry, I think I was wrong in my previous comment. I believe the reason it failed was because you used the wrong surname the first time.

You do not need to .send() both the name and surname; you only need to .send() the surname. I believe this is why you (correctly) passed the previous lesson.

Let me know if this makes sense, or if you have any other questions!

1 Like

Hey, i am facing similar situation here. but with me only first 2 cases are getting passed. can you help me with this.

 test('send {name: "Giovanni", surname: "da Verrazzano"}', function(done) {
        /** place the chai-http request code here... **/
        chai
          .request(server)
          .put("/travellers")
          .send({
            name: "Giovanni",
            surname: "da Verrazzano"
          })
          .end(function(err, res) {
            /** place your tests inside the callback **/
            assert.equal(res.status, 200, "response status should be 200");
            assert.equal(
              res.type,
              "application/json",
              "Response should be json"
            );

            assert.equal(
              res.body.name,
              "Giovanni",
              'res.body.name should be "Giovanni"'
            );
            assert.equal(
              res.body.surname,
              "da Verrazzano",
              'res.body.surname should be "da Verrazzano"'
            );

            done();
          });
      });

test currently broken. copy the test answer into the box and it wont pass tests.

any updates? it seems like it’s still broke?

and the problem was a missing }); at the bottom I feel so dumb now