-
Notifications
You must be signed in to change notification settings - Fork 477
how to write test code for delegator contract #747
Copy link
Copy link
Closed
Labels
C-questionFurther information is requestedFurther information is requested
Description
Questions
when I want to test delegator contract function, I got error, when I use mockall, also got compile error
test code is here
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
#[ink::contract]
mod delegator {
use accumulator::Accumulator;
use adder::Adder;
use ink_storage::{
traits::{
PackedLayout,
SpreadLayout,
},
Lazy,
};
use subber::Subber;
/// Specifies the state of the delegator.
///
/// In `Adder` state the delegator will delegate to the `Adder` contract
/// and in `Subber` state will delegate to the `Subber` contract.
///
/// The initial state is `Adder`.
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
scale::Encode,
scale::Decode,
SpreadLayout,
PackedLayout,
)]
#[cfg_attr(
feature = "std",
derive(::scale_info::TypeInfo, ::ink_storage::traits::StorageLayout)
)]
pub enum Which {
Adder,
Subber,
}
/// Delegates calls to an adder or subber contract to mutate
/// a value in an accumulator contract.
///
/// In order to deploy the delegator smart contract we first
/// have to manually put the code of the accumulator, adder
/// and subber smart contracts, receive their code hashes from
/// the signalled events and put their code hash into our
/// delegator smart contract.
#[ink(storage)]
pub struct Delegator {
/// Says which of adder or subber is currently in use.
which: Which,
/// The accumulator smart contract.
accumulator: Lazy<Accumulator>,
/// The adder smart contract.
adder: Lazy<Adder>,
/// The subber smart contract.
subber: Lazy<Subber>,
}
impl Delegator {
/// Instantiate a delegator with the given sub-contract codes.
#[ink(constructor)]
pub fn new(
init_value: i32,
accumulator_code_hash: Hash,
adder_code_hash: Hash,
subber_code_hash: Hash,
) -> Self {
let total_balance = Self::env().balance();
let accumulator = Accumulator::new(init_value)
.endowment(total_balance / 4)
.code_hash(accumulator_code_hash)
.instantiate()
.expect("failed at instantiating the `Accumulator` contract");
let adder = Adder::new(accumulator.clone())
.endowment(total_balance / 4)
.code_hash(adder_code_hash)
.instantiate()
.expect("failed at instantiating the `Adder` contract");
let subber = Subber::new(accumulator.clone())
.endowment(total_balance / 4)
.code_hash(subber_code_hash)
.instantiate()
.expect("failed at instantiating the `Subber` contract");
Self {
which: Which::Adder,
accumulator: Lazy::new(accumulator),
adder: Lazy::new(adder),
subber: Lazy::new(subber),
}
}
/// Returns the accumulator's value.
#[ink(message)]
pub fn get(&self) -> i32 {
self.accumulator.get()
}
/// Delegates the call to either `Adder` or `Subber`.
#[ink(message)]
pub fn change(&mut self, by: i32) {
match self.which {
Which::Adder => self.adder.inc(by),
Which::Subber => self.subber.dec(by),
}
}
/// Switches the delegator.
#[ink(message)]
pub fn switch(&mut self) {
match self.which {
Which::Adder => {
self.which = Which::Subber;
}
Which::Subber => {
self.which = Which::Adder;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use ink_lang as ink;
#[ink::test]
pub fn default_works() {
let mut delegator = Delegator::new(1, Hash::from([0x99; 32]),
Hash::from([0x99; 32]), Hash::from([0x99; 32]));
delegator.change(2);
}
}
}then run cargo test, got error
running 1 test
test delegator::tests::default_works ... FAILED
failures:
---- delegator::tests::default_works stdout ----
thread 'delegator::tests::default_works' panicked at 'not implemented: off-chain environment does not support contract instantiation', /Users/jacksenguo/RustProjects/apron-contracts/ink/crates/env/src/engine/off_chain/impls.rs:373:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
delegator::tests::default_worksReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
C-questionFurther information is requestedFurther information is requested