Can not assert the creation of a user


#1

I want to test my endpoint.

In my controller I have:

async index()  {
  return await User.all()
}

In my test, I have:


test('Can add a user', async( {assert, client}) => {                                                                                          
  const user = await Factory.model('App/Models/User').create()                                                                             
  const response = await client.get('users')  // I get all users, array of objects                                                                                 
  assert.include(response, user.username)                                                                                                    
})

I get this error message:

ERRORS

  1. Can add a user
    AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given
    at Callable.test

When I tried this:


test('Can add a user', async( {assert, client}) => {                                                                                          
  const user = await Factory.model('App/Models/User').create()                                                                             
  const response = await client.get('users')  // I get all users, array of objects
  const lastUser = response.slice(-1)[0]  // I want the last user, object                                                                                     
  assert.include(lastUser.name, user.username)                                                                                                    
})

I get this error message:

TypeError: response.slice is not a function

When I test my endpoint with curl, I get the list of users, also asserting the status code to be 200 works if I put it in the above test. What am I doing wrong?


#2

By your description you appear to be testing the create() method which is part of core Adonis? No reason to test that method, virk has it covered. Are you trying to test your endpoint?

I use a different syntax although probably makes no difference

return response.json(users);

My test may read something very simple like

test("get user list", async ({ assert, client }) => {
    await User.query().truncate();
    await User.create(my_user_object);

    var response = await client.get("/users").end();
    response.assertStatus(200);
   response.assertJSONSubset(my_user_object);

}).timeout(0);

I am only testing the URL isn’t broken. You may need to append the primary key value returned from the create command to fulfil the JSONSubset assertion.


#3

Thank you.
I want to test that when I add a user, the endpoint shows it up.
In other words, I want to make sure the user creation is always successful.
Otherwise yes, assertStatus() is enough to test for the existence of that endpoint.


#4

No reason to test that method, virk has it covered.

That is a good point too.

But then I wonder why they wrote the second test here.


#5

should see post on home page

The clue is in the title :slight_smile: It is not the create() method being tested.


#6

That is a bit philosophy because if the post is created then sure it has a title, but then it is useless to test the creation of a post since Virk did it :slight_smile:

One last thing if I may: how come that test passes? I mean this line assert.include(response.text, post.title) Because response is an array of object. My test is almost the same but it fails

I mean if I run this test:

const response = [{name: 'begueradj'}, {name: 'jackson'}]
assert.include(response.name,  'begueradj')

It will fail.


#7

I did the same mistake as last night: I forgot the.end() function:

const response = await client.get('users') .end()

the assertion will work then because everything is inside the response.text