How to test a route having a parameter when using Route.resource()


#1

How to test a route having a parameter?

Note that I created my routes this way: Route.resource('users', UserController')

This will generate these endpoints:

// ...equates to this:
Route.get('users', 'UserController.index').as('users.index')
Route.post('users', 'UserController.store').as('users.store')
Route.get('users/create', 'UserController.create').as('users.create')
Route.get('users/:id', 'UserController.show').as('users.show')
Route.put('users/:id', 'UserController.update').as('users.update')
Route.patch('users/:id', 'UserController.update')
Route.get('users/:id/edit', 'UserController.edit').as('users.edit')
Route.delete('users/:id', 'UserController.destroy').as('users.destroy')

I have a problem with the create function:

'use strict'                                                                                                                                     
                                                                                                                                                 
class UserController {                                                                                                                   
                                                                                                                                                 
  edit( {params} ) {                                                                                                                             
    return 'hi'                                                                                                                                  
  }                                                                                                                                              
}  

My test:

test('Route /users/:id/edit responds to GET', async ( { client } ) => {                                                                          
  const response = await client.get('/users/edit').end()                                                                                         
  response.assertStatus(200)                                                                                                                     
})  

I am getting a 500 HTTP error code.

What am I doing wrong here?

Repository if you want to reproduce this. Instruction are in the README file.


#2

Hey @begueradj! :wave:

You forget to set the id on your test.

// Wrong URL
client.get('/users/edit').end() 

// Correct URL
client.get(`/users/${id}/edit`).end() 

The id variable should came from a user you create via a Factory.


#3

Thank you.

Here is my database/factory.js:

const Factory = use('Factory')                                                                                                                   
const Hash = use('Hash')                                                                                                                         
                                                                                                                                                 
Factory.blueprint('App/Models/User', async (faker) => {                                                                                          
  return {                                                                                                                                       
    username: faker.username(),                                                                                                                  
    email: faker.email(),                                                                                                                        
    password: await Hash.make(faker.password())                                                                                                  
  }                                                                                                                                              
})

Here is my updated test:


test('Route /users/:id/edit responds to GET', async ( { client } ) => {                                                                          
  const user = await Factory                                                                                                                     
   .model('App/Models/User')                                                                                                              
   .create()                                                                                                                              
  let id = user.id                                                                                                                               
  const response = await client.get(`/users/${id}/edit`).end()                                                                                   
  response.assertStatus(200)                                                                                                                     
})

Still getting a 500 @romain.lanz

I also updated my repo just in case (link in my question)


#4

This is already solved I guess? The repo is a bit on the messy side but your tests all seem to pass for me…

Selection_009


#5

I spent a long time wondering what is going on … my god … you are right, all is Ok.
The issue was that I have a dozen of Emacs buffers open on the same time and I was testing running the tests in a wrong place
Thank you so much @pseudolog