0

When I run cargo test-bpf --manifest-path=./Cargo.toml on this code

#[tokio::test]
async fn test_init_mapping() {
    let program_id = Pubkey::new_unique();
    let mut init_map_test = ProgramTest::new(
        "simple", // Run the BPF version with `cargo test-bpf`
        program_id,
        processor!(process_instruction), 
    );
    let main_pubkey = Pubkey::new_unique();
    let main_account = Account {
        lamports: 100,
        owner: program_id,
        ..Account::default()
    };
    init_map_test.add_account(main_pubkey, main_account);
    let (mut banks_client, payer, recent_blockhash) = init_map_test.start().await;
}

I get this error.

thread 'test_init_mapping' panicked at 'Invoke context not set!'**

I don't know how to debug this, so any help is great. I have narrowed the problem down to the last line. I must have something else set up wrong somewhere in my project?

I dont think this code is a problem as its copy pasted from the helloworld example.

Edit: I left vital details from the original question. After the last line I had msg!("started"); , I assumed irrelevant so left it out of the question

wxecqz123
  • 325
  • 1
  • 5
  • 12
  • Typically, that error is triggered when the program is actually run. Unfortunately, I copied this code and it works for me using the latest 1.7.10 release. What version of the SDK and CLI are you using? – Jon C Aug 13 '21 at 03:32
  • hi Jon. Im using 1.6.10 because I have copied metaplex github project to try and follow the structure. Do you know of a project in 1.7.10 which i can clone? When I try to run this script again using 1.7.10 I get an error saying "failed to install bpf-tools" – wxecqz123 Aug 14 '21 at 07:50
  • not sure what is the issue was. I have put up this on github which is working for me https://github.com/antonnewcombe/nothing – wxecqz123 Aug 14 '21 at 09:29

1 Answers1

5

Your edit is the crucial part here:

Edit: I left vital details from the original question. After the last line I had msg!("started"); , I assumed irrelevant so left it out of the question

If you look at the code for the msg! macro, it just calls sol_log, which in a ProgramTest environment, becomes this call: https://github.com/solana-labs/solana/blob/7a8807b8bba2a0bd41f696d3309487d6423a0b4b/program-test/src/lib.rs#L227

msg! should only be called from within a program, where the invoke_context is set, which is why you are running into this issue.

To fix this, remove the call to msg! and use a simple println!. From within your program, however, feel free to use msg! as much as you want.

Jon C
  • 7,019
  • 10
  • 17