starknet cairo 测试报错 [ERROR] Failed to build test artifacts with Scarb: `scarb` exited with error

image.png

代码 src/counter.cairo

#[starknet::interface]
trait ICounter<TContractState> {
    fn get_counter(self: @TContractState) -> u32;
}

#[starknet::contract]
mod Counter {
    use super::ICounter;

    #[storage]
    struct Storage {
        counter: u32
    }

    #[constructor]
    fn constructor(ref self: ContractState, counter: u32) {
        self.counter.write(counter);
    }

    #[abi(embed_v0)]
    impl ICounterImpl of ICounter<ContractState> {
        fn get_counter(self: @ContractState) -> u32 {
            self.counter.read()
        }
    }
}

src/lib.cairo

mod Counter;

测试代码 tests/test_step.cairo

use super::utils::deploy_contract;
use counter::counter::{ICounterDispatcher, ICounterDispatcherTrait};

#[test]
fn check_stored_counter() {
    let initial_counter = 12;
    let contract_address = deploy_contract(initial_counter);
    let dispatcher = ICounterDispatcher { contract_address };
    let stored_counter = dispatcher.get_counter();
    assert!(stored_counter == initial_counter, "Stored value not equal");
}

tests/utils.cairo

use starknet::{ContractAddress};
use snforge_std::{declare, cheatcodes::contract_class::ContractClassTrait};

pub fn deploy_contract(initial_value: u32) -> ContractAddress {
    let contract = declare("Counter").unwrap();
    let constructor_args = array![initial_value.into()];
    let (contract_address, _) = contract.deploy(@constructor_args).unwrap();
    contract_address
}

tests/lib.cairo

mod test_step;
mod utils;

Scarb.toml

[package]
name = "counter"
version = "0.1.0"
edition = "2023_01"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet = ">=2.6.3"

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.27.0" }

[[target.starknet-contract]]

[scripts]
test = "snforge test"

image.png

为什么会测试失败?

counter-workshop on  step4 [!] via ? base 
➜ scarb test       
     Running test counter (snforge test)
error: Identifier not found.
 --> /Users/qiaopengjun/Code/cairo/counter-workshop/tests/test_step.cairo:2:14
use counter::counter::{ICounterDispatcher, ICounterDispatcherTrait};
             ^*****^

error: Identifier not found.
 --> /Users/qiaopengjun/Code/cairo/counter-workshop/tests/test_step.cairo:2:14
use counter::counter::{ICounterDispatcher, ICounterDispatcherTrait};
             ^*****^

error: Method `get_counter` not found on type `<missing>`. Did you import the correct trait and impl?
 --> /Users/qiaopengjun/Code/cairo/counter-workshop/tests/test_step.cairo:9:37
    let stored_counter = dispatcher.get_counter();
                                    ^*********^

warning: Unused variable. Consider ignoring by prefixing with `_`.
 --> /Users/qiaopengjun/Code/cairo/counter-workshop/tests/test_step.cairo:7:9
    let contract_address = deploy_contract(initial_counter);
        ^**************^

Error: Failed to compile test artifact, for detailed information go through the logs above
[ERROR] Failed to build test artifacts with Scarb: `scarb` exited with error
请先 登录 后评论

1 个回答

寻月隐君
请先 登录 后评论
  • 1 关注
  • 0 收藏,597 浏览
  • 寻月隐君 提出于 2024-08-06 20:56