How to use Refresh Database like on laravel


#1

Hi guys.

The title says it all, I’m new in this web framework. How can i do the refreshdatabase like on laravel in adonis

in Laravel

use Illuminate\Foundation\Testing\RefreshDatabase;
class SampleTest extends TestCase 
{
   use RefreshDatabase;
   ....

My code

'use strict'

const { test, trait } = use('Test/Suite')('Fetch States')
const States = use('App/Models/State')
const Factory = use('Factory')

trait('Test/ApiClient')

test('get list of states', async ({ client }) => {

	let states = await Factory
	  		.model('App/Models/State')
	  		.create()


	const response = await client.get('api/states').end()


	response.assertStatus(200)
})

Thanks guys.


#2

Refresh means?

You can use the transactions trait. http://adonisjs.com/docs/4.1/testing-fakes#_database_transactions


#3

I already tried it, but it seems it doesn’t rollback my data on my database.


#4

Hey @ronmrcdo! :wave:

In the example that you give us above, you aren’t using this trait.


#5

Sorry my code above is not updated. But I already tried it and it doesn’t seems to work. Here’s my updated code.

'use strict'

const { test, trait } = use('Test/Suite')('Fetch States')
const States = use('App/Models/State')
const Factory = use('Factory')

trait('DatabaseTransactions')
trait('Test/ApiClient')

test('get list of states', async ({ client }) => {

	let states = await Factory
	  		.model('App/Models/State')
	  		.create()

	const response = await client
				.get('api/states')
				.end()

	response.assertStatus(200)
	//response.assertJSON(states)
})

Thanks.


#6

Now what doesn’t work here?


#7

The test works fine but the database doesn’t refresh ( I mean rollback to no data ) so that every test the data will be gone.

Sorry, I’m new to adonisjs. Thanks for reply


#8

Hi, i also working in this kind of code. I registered the DatabaseTransactions trait. Im using mongo but it gives me an error of i need to install sqlite3. How can i fix it? Thanks


#9

Make sure in .env.testing you have set up mongo as well, rathern than sqlite.


#10

Thank you :slight_smile: