How to set new_user to false for unit testing


#1

For unit tests where I want to start as a returning user should I go through the new user flow first, always keep a user in the db and use setUserId() to reference the user or is there another option?


#2

You could set a fixed user id:

const conversation = testSuite.conversation({
          userId: 'myUserId`
});

#3

In addition to @AlexSwe’s approach, here’s what I do: I simulate a returning user by creating a short first session that I dismiss, and then start the actual session that I want to use for unit testing:

// First dummy session
let timestamp = '2019-04-14T19:25:57Z';
let launchRequest = await testSuite.requestBuilder.launch();
launchRequest.setTimestamp(timestamp);
let launchResponse = await conversation.send(launchRequest);
let intentRequest =  await testSuite.requestBuilder.intent();
intentRequest.setIntentName('DummyIntent');
let intentResponse = await conversation.send(intentRequest);

// Actual session
timestamp = '2019-04-15T19:25:57Z';
launchRequest.setTimestamp(timestamp);
launchResponse = await conversation.send(launchRequest);
intentRequest.setTimestamp(timestamp);
intentRequest.setIntentName('TestIntent');
intentResponse = await conversation.send(intentRequest);

What I like about this approach is that you arrive at the second session naturally by traversing the Skill, rather than by setting variables.

Hope this helps! :smiley:


#4

Thank you - this makes a lot of sense.