简体   繁体   中英

Mocha Unit testing without data

I'm not used to write unit tests, but want to improve myself when it comes to testing. Currently I'm writing an app (basically just a REST API) with a small SQLite database. How to write tests against the database if there's no data?

Assuming a GET request to /api/posts returns all posts as json

[
 { id: 1, title: 'Test' },
 { id: 2, title: 'Blah' }
]

So my test looks like

  describe('GET /', function () {
    it('should return some posts', function (done) {
      request(server)
        .get('/api/posts')
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) {
          if(err) {
            throw err;
          }

          res.body.should.be.instanceof(Array);
          // ... hmmm?

          done();
        });
    });
  });

But how to check the data itself (assuming there are no posts in the database at this point)? I'd like to check if the attributes (title, ...) are given, if passed parameters (like limit, etc.) return expected results (eg limited reults, ...), etc..

I think you are looking for something like Fixtures and Mock Ajax, where you can use temporary data and fake requests to simulate the live environment.

Check this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM