samedi 22 août 2020

v4.1: How to create BaseTest resources?

in order to not repeat code in my tests I want to write some common functions, such as: actingAsAdmin where I create (or get an existing one) a customer, create a user, create and admin role and attach it to that user. Ths way in my tests a can say

let user = actingAsAdmin()
//test that admin can access this resource

let normalUser = actingAsUser()
//test that normal user can't access this resource

In Laravel I would just extend the IntegrationBaseTestCase and add all those helper functions, but in adonis I don't know how to do it.

I'm using @adonisjs/vow package. I saw that I can define suites and use this

const { trait, test } = use('Test/Suite')('Sample test suite')

trait((suite) => {
  suite.Context.getter('foo', () => 'bar')
})


test('foo is bar', (ctx) => {
  ctx.assert(ctx.foo, 'bar')
})

to create something like I want. But I want this code to be general. How can I achieve this?

Aucun commentaire:

Enregistrer un commentaire