How to store information from a test to reuse it
LandyCuadra opened this issue · 4 comments
Hi, I am trying to do some test over my backend in the example below I create a client and it returns an object containing an id, after that I test the method to get client by Id... but would like to reuse the id I got from the last method.... is there a way to do that?
I tried creating a variable called clientId assign it in the save response then try to call it in get method url, but I get null in the var as I define it on creation...
`
let clientId = null;
TEST('Test Save Client', '/client', function(builder){
builder.method('post');
builder.json({
'address': 'TEST place',
'email': 'test@email.com',
'name': 'TEST name'
});
builder.exec(function(err, response){
clientId = response.value?.newClient;
OK(response.success === true, 'response success equals true');
OK(typeof response.value === 'object', 'response value is type object');
OK(response.value instanceof Object, 'response value is instance of object');
OK(response.value?.newClient, 'newClient has truthy value');
OK(Object.prototype.hasOwnProperty.call(response.value, 'newClient'), 'response value newClient node');
OK(Object.keys(response.value).length > 0, 'response value is not empty');
});
});
TEST('Test Get Client By Id', /client/${clientId}
, function(builder){
builder.method('get');
builder.exec(function(err, response){
OK(response.success === true, 'response success equals true');
OK(typeof response.value === 'object', 'response value is type object');
OK(response.value instanceof Object, 'response value is instance of object');
OK(Object.keys(response.value).length > 0, 'response value is not empty');
});
});
`
Hi. Try to declare a new TEST()
into the callback of the TEST()
method. Total.js v4 supports better unit-testing: https://docs.totaljs.com/total4/40842001ok51c/
how can I achieve that in Total.js V4, because I don't see a way nest test in the documentatition?
We will update documentation.
- unit-testing in Total.js v4 works only with routing to Total.js Schemas
- you can define nested tests like this:
self.runtest('API /apiv1/ users_read').callback(function(err, value) {
self.runtest(...);
self.runtest(...);
self.runtest(...);
});
We will improve documentation for testing next week, it's not good right now.
Thank you for everything, it works well. will check if I can upgrade the version to V4