Use dubhe to test a contract

We’ll start by creating a test file.

touch contracts/counter/sources/tests/message.move

deploy_dapp_for_testing is the test method that Dubhe automatically generates for you. Let’s write the test.

#[test_only]
module dms::message_test {
   use sui::test_scenario;
   use std::ascii::string;
   use dms::message_system;
   use dms::message_schema::Message;
   use dms::init_test;
   #[test]
   public fun send() {
       let (mut scenario, dapp)  = init_test::deploy_dapp_for_testing(@0xA);
       let mut message = test_scenario::take_shared<Message>(&scenario);
       assert!(message.content().get() == string(b"Hello, World!"));

       let ctx = test_scenario::ctx(&mut scenario);
       message_system::send(&mut message, string(b"Move, Move!"), ctx);
       assert!(message.content().get() == string(b"Move, Move!"));

       test_scenario::return_shared(message);
       dapp.distroy_dapp_for_testing();
       scenario.end();
   }
}

Let’s run the Test command to check for syntax errors.

âžś  dubhe-template-project pnpm dubhe test
🚀 Running move test
INCLUDING DEPENDENCY Dubhe
INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING dms
Running Move unit tests
[ PASS    ] dms::message_test::send
Test result: OK. Total tests: 1; passed: 1; failed: 0
Total number of linter warnings suppressed: 1 (unique lints: 1)