How to write a test file to test things like “30 days expire”?

I want to write some test cases for my web application, but got stuck in some cases, such as "This token should be expired after 30 days." Nobody likes to wait 30 days before the end of the tests.

There are also other cases related to some planned events, such as “Send this email two weeks after user registration”, “Form an invoice 2 days before the next billing date”, etc.

What sentence do you have written in such cases? Or, is there another way to make sure that these functions work as accurately as intended?

+6
source share
3 answers

My approach when writing these tests is to determine the expiration in milliseconds. This way, you can easily write tests because your test environments can detect 1 ms expiration. The production environment will obviously determine the appropriate duration. They can usually be set as configuration values ​​or db values.

+4
source

I assume you have code like

if(token.ExpirationDateTime > DateTime.Now) { // do some job to expire } 

I usually provide classes with this logic with an input field of type Func<DateTime> . for instance

 public class ExpirationManager { private Func<DateTime> _nowProvider; public ExpirationManager(Func<DateTime> nowProvider) { _nowProvider = nowProvider; } } 

Then the expiration code looks like

 if(token.ExpirationDateTime > _nowProvider()) { // do some job to expire } 

Thus, you can replace the real current system time with any DataTime that you want when you run tests (for example, unit tests).

+1
source

Break the functionality down and test the individual steps.

The expiration of an element after a certain time consists in placing the element somewhere with an expiration mark and another part of the program expiring with elements with an old time stamp. Accordingly, check that the new items with the correct time stamp are inserted correctly and that the expiration code correctly removes items that are 30 days old. An element inserted and deleted does not have to be the same for the test.

The same applies to e-mail sent at a certain interval.

0
source

Source: https://habr.com/ru/post/912286/


All Articles