Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 134 characters in body
Source Link
mbsff
  • 93
  • 6

My contract has time dependent actions, which I want to test. How can I mock the clock in the smart contract such that I can set it to any time I want?

An example of one action that is time dependent is as follows:

void claimsampleact(const account_name user,
           const int32_t day) {
    // Check for user authentication.
    require_auth(user);
    // Check if passed day is between the start and end.
    eosio_assert(daygetToday() > 0 && day < getToday() && day <= getTotalDays()30, "Invalid day");
    // getToday() returns floor(current_time() - start_time) / (24 * 3600). 
    // I WANT TO MOCK CURRENT_TIME().

   ...
   code
   ...

}

Then in the unit test I want to do the following.

  • test sampleact().
  • advance the blockchain clock 31 days.
  • test sampleact(), which will fail the assert seen above.

My contract has time dependent actions, which I want to test. How can I mock the clock in the smart contract such that I can set it to any time I want?

An example of one action that is time dependent is as follows:

void claim(const account_name user,
           const int32_t day) {
    // Check for user authentication.
    require_auth(user);
    // Check if passed day is between the start and end.
    eosio_assert(day > 0 && day < getToday() && day <= getTotalDays(), "Invalid day");
    // getToday() returns floor(current_time() - start_time) / (24 * 3600). 
    // I WANT TO MOCK CURRENT_TIME().

   ...
   code
   ...

}

My contract has time dependent actions, which I want to test. How can I mock the clock in the smart contract such that I can set it to any time I want?

An example of one action that is time dependent is as follows:

void sampleact(const account_name user) {
    // Check for user authentication.
    require_auth(user);
    // Check if passed day is between the start and end.
    eosio_assert(getToday() > 0 && getToday() <= 30, "Invalid day");
    // getToday() returns floor(current_time() - start_time) / (24 * 3600). 
    // I WANT TO MOCK CURRENT_TIME().

   ...
   code
   ...

}

Then in the unit test I want to do the following.

  • test sampleact().
  • advance the blockchain clock 31 days.
  • test sampleact(), which will fail the assert seen above.
Source Link
mbsff
  • 93
  • 6

Time dependent EOS unit test

My contract has time dependent actions, which I want to test. How can I mock the clock in the smart contract such that I can set it to any time I want?

An example of one action that is time dependent is as follows:

void claim(const account_name user,
           const int32_t day) {
    // Check for user authentication.
    require_auth(user);
    // Check if passed day is between the start and end.
    eosio_assert(day > 0 && day < getToday() && day <= getTotalDays(), "Invalid day");
    // getToday() returns floor(current_time() - start_time) / (24 * 3600). 
    // I WANT TO MOCK CURRENT_TIME().

   ...
   code
   ...

}