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


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')'users', '').as('')
Route.get('users/create', 'UserController.create').as('users.create')
Route.get('users/:id', '').as('')
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()                                                                                         

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.


Hey @begueradj! :wave:

You forget to set the id on your test.

// Wrong URL

// Correct URL

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


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(),                                                                                                                  
    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                                                                                                                     
  let id =                                                                                                                               
  const response = await client.get(`/users/${id}/edit`).end()                                                                                   

Still getting a 500 @romain.lanz

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


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



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