2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
|
title: Run Functional Tests on API Endpoints using Chai-HTTP
|
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Run Functional Tests on API Endpoints using Chai-HTTP
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
|
## Problem Explanation
|
2019-07-02 07:00:18 +10:00
|
|
|
To begin, open the file "tests/2_functional_tests.js" and locate 'Test GET /hello with no name'.
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
## Hints
|
|
|
|
|
|
|
|
|
|
### Hint 1
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-02 07:00:18 +10:00
|
|
|
Using the example above, look at the assertions and how they are making comparisons between the expected and actual values of the response.
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
### Hint 2
|
2019-07-02 07:00:18 +10:00
|
|
|
|
|
|
|
|
The lines in the test should be changed from `assert.fail()` to an assertion that checks if the two values are equal.
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
## Solutions
|
|
|
|
|
|
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
2019-07-02 07:00:18 +10:00
|
|
|
|
|
|
|
|
```js
|
2019-07-24 00:59:27 -07:00
|
|
|
test('Test GET /hello with no name', function(done) {
|
|
|
|
|
// Don't forget the callback...
|
|
|
|
|
chai
|
|
|
|
|
.request(server) // 'server' is the Express App
|
|
|
|
|
.get('/hello') // http_method(url). NO NAME in the query !
|
|
|
|
|
.end(function(err, res) {
|
|
|
|
|
// res is the response object
|
|
|
|
|
|
|
|
|
|
// Test the status and the text response (see the example above).
|
|
|
|
|
// Please follow the order -status, -text. We rely on that in our tests.
|
|
|
|
|
// It should respond 'Hello Guest'
|
|
|
|
|
assert.equal(res.status, 200);
|
|
|
|
|
assert.equal(res.text, 'hello Guest');
|
|
|
|
|
done(); // Always call the 'done()' callback when finished.
|
|
|
|
|
});
|
2019-07-02 07:00:18 +10:00
|
|
|
});
|
2019-07-24 00:59:27 -07:00
|
|
|
```
|
|
|
|
|
</details>
|