Simple Contract With Remix
Create and Test your contract with Remix
The new version of Remix is out. Let’s learn how to use the new interface with a Simple Contract.
Create a new contract called “SimpleStorage.sol” inside the file explorer.
This simple contract defines a string named value that gets initialized in the constructor and gets saved in the storage. This string is updated with the method setValue
. When the value is updated, it emits an event ValueChanged
.
But… It doesn’t compile ??? And for that matter, where do you click a button to compile it???
Remix is now divided into Plugins that can be loaded individually. This makes Remix more customizable. So, for example, if you want to use Vyper, you don’t need Solidity compiler.
Compile your Contract
Click on the “PluginManager” icon on the left bar (it’s the one that looks like a plug). Here you’ll see all available Plugins inside Remix.
Activate “Solidity Compiler”.
Click on the new solidity icon to open the Solidity Compiler panel. Hit “Compile”, or just click inside the editor and hit “Ctrl/Cmd+s”.
Test your Contract
Now that your contract is compiled, you’ll want to verify that it works. Let’s use the Remix Test Plugin for that.
Under the Plugin Manager panel, activate “Solidity Unit Testing”. You should now have two plugins activated :
Inside the “Solidity Unit Testing” panel you will find the documentation and examples to build your solidity tests.
Let’s write our solidity test file from scratch.
Go back to the FileExplorer and create a SimpleStorage_test
contract (the name needs to end with _test
to be considered as a testing contract):
First we import the remix_tests.sol
library as well as our contract ./SimpleStorage.sol
. Then we create the contract and define a variable testContract
that will be reassigned before each test.
It’s important to use
beforeEach
because the library doesn’t run the tests by order of definition, but by alphabetic order. In this caseshouldChangeValue
will be test beforeshouldHaveAValue
.
The library exposes the method equal
that check if the two first parameter are equal, and display the third if not.
Run the tests
Go back to the Solidity Unit Testing panel. Check you contract, and hit “Run Tests” :
As you can see in this example, the first test is “Should change value”, and the second one is “Should have a value”.