7
7
700ghz2018-12-04 12:34:20
API
700ghz, 2018-12-04 12:34:20

How to properly populate the database when testing API?

I am writing tests for my API.
Here is where I test POST /users/:userId/item:

it('should add item to user', async () => {
  await myTestsHelper.cleanDb();

  // создаю юзера в БД. Мой вопрос про вот эту функцию :) 
  const user = await myTestsHelper.factories.createSomeUser();

  // вызываю тестируемый эндпоинт
  await request.post(`/users/$(user.id)/item`)
  .expect(200);
  // and expect bla bla bla ... Тут все понятно :) 
})

What should be inside myTestsHelper.factories.createSomeUser function ?
The fact is that creating a user in my system is "magic calculations" and filling in a bunch of tables (user, billing, settings, images). The user creation functionality is in my POST /user
endpoint. I see three ways to implement myTestsHelper.factories.createSomeUser:
  1. http request to POST /user (best way)
  2. adding records to tables (like emulating the result of POST /user operation) (complicating test code, difficult to maintain)
  3. try to pull out the addUser function from the POST /user endpoint and pull it in tests
    (what?? for the sake of testing, change the structure of the application, tear out the function??)

All three methods will work. But what is right? Is option 1 good?
Thank you in advance!
PS nodejs, expressjs, mocha, tdd

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2018-12-04
@700ghz

What level of tests?
3 point of course not to do!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question