testi za sol

mail@pastecode.io avatar
unknown
javascript
a year ago
2.0 kB
2
Indexable
Never
const assert = require("assert");                                             // Import the 'assert' module for making assertions in the test
const truffleAssert = require("truffle-assertions");                           // Import the 'truffle-assertions' module for making event-related assertions

const MySmartContract = artifacts.require("MySmartContract");                  // Import the compiled smart contract artifact

contract("MySmartContract", accounts => {                                      // Define the test suite for the smart contract
  let contractInstance;                                                        // Declare a variable to store the instance of the contract

  beforeEach(async () => {                                                     // This function runs before each test
    contractInstance = await MySmartContract.new();                            // Create a new instance of the contract
  });

  it("should set the value correctly", async () => {                           // Define the first test
    await contractInstance.set(5, { from: accounts[0] });                      // Call the 'set' function with the value of 5
    const result = await contractInstance.get();                               // Call the 'get' function to retrieve the value
    assert.equal(result.toNumber(), 5, "The value was not set correctly");      // Use 'assert.equal' to check that the returned value is equal to 5
  });

  it("should emit an event when the value is set", async () => {              // Define the second test
    const transaction = await contractInstance.set(5, { from: accounts[0] });  // Call the 'set' function with the value of 5
    truffleAssert.eventEmitted(transaction, "ValueSet", event => {            // Use 'truffleAssert.eventEmitted' to check if the 'ValueSet' event is emitted
      return event.newValue.toNumber() === 5;                                  // Check that the 'newValue' property of the event is equal to 5
    });
  });
});