1

I am implementing a project using the directory structure proposed by openbrush. The contract compiles without a problem, but when it comes to testing I run into the following error:

error[E0599]: no function or associated item named `new` found for trait object `dyn project_name::traits::traitName::TraitName` in the current scope
   --> tests/integrations_tests.rs:148:30
    |
148 |             let contract = TraitName::new(input);
    |                              ^^^ function or associated item not found in `dyn project_name::traits::traitName::TraitName`

Do I need to declare the constructor in the trait somewhere?

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]

#[openbrush::contract]
pub mod name {
    use project_name::traits::traitName::*;
  
    impl TraitName {
        #[ink(constructor)]
        pub fn new(input: bool) -> Self {
            ink_lang::codegen::initialize_contract(|contract: & mut Self| {
                Self::new_init(contract, input)
            })
        }

        fn new_init(&mut self, input: bool) {
            // do smth with input
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use ink_lang as ink;

        #[ink::test]
        fn test_nr_1(){
            let contract = TraitName::new(true);
            // stuff to test with contract
        }
        
    }

}
2
  • can you provide the code of contract you want to test? the new function (or ink constructor) should be inside the implementation of the contract storage struct
    – kriko.eth
    Sep 12, 2022 at 9:12
  • Sure, sry about that. I adjusted the error message and provided an example how I structured my code.
    – rajohs
    Sep 12, 2022 at 14:17

1 Answer 1

2

A solution to the problem was to not have the tests in a seperate test module.

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]

#[openbrush::contract]
pub mod name {
    use project_name::traits::traitName::*;
  
    impl TraitName {
        #[ink(constructor)]
        pub fn new(input: bool) -> Self {
            ink_lang::codegen::initialize_contract(|contract: & mut Self| {
                Self::new_init(contract, input)
            })
        }

        fn new_init(&mut self, input: bool) {
            // do smth with input
        }
    }

    
    use ink_lang as ink;

    #[ink::test]
    fn test_nr_1(){
        let contract = TraitName::new(true);
        // stuff to test with contract
    }
        
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.