#Returning a mock from a `mockall` expectation, and injecting it later.

6 messages · Page 1 of 1 (latest)

topaz patrol
#
    #[test]
    fn test_fix_success() {
        let mut mock_ssh_config = MockSshConfigTrait::new();
        mock_ssh_config
            .expect_session()
            .with(eq("host"))
            .return_once(|_| {
                let mut mock_ssh_session = MockSshSessionTrait::new();
                mock_ssh_session
                    .expect_exec()
                    .times(4)
                    .with(eq("chown -c uid:gid ~uid 2>&1"))
                    .with(eq("chown -c -R uid:gid ~uid/website 2>&1"))
                    .with(eq(
                        "find ~uid/website -type d -print0 | xargs -0 chmod -c 2750 2>&1",
                    ))
                    .with(eq(
                        "find ~uid/website -type f -print0 | xargs -0 chmod -c 0640 2>&1",
                    ))
                    .returning(|_| Ok("".to_string()))
                    .returning(|_| Ok("".to_string()))
                    .returning(|_| Ok("".to_string()))
                    .returning(|_| Ok("".to_string()));
                Ok(Arc::new(mock_ssh_session) as DynSshSession)
            });
        let model = PermsfixModel {
            ssh_config: Arc::new(mock_ssh_config) as DynSshConfig,
        };
        let result = model.fix("host", "website", "uid", "gid").unwrap();
        assert_eq!(result, "".to_string());
    }

In this test, I'm passing in mock_ssh_config in PermsFixModel, with the expectation that it will produce a mock_ssh_session, which later on will be expected to execute some commands.

With my limited experience, the result is not what I expect. I expect SshConfig::session() to be mocked and return the given mock. However, the method is actually invoked. I have asserted this with a simple debiug statement in SshConfig::session().

What am I doing wrong here? Is there a better approach?

Thanks a bunch,
Samy

river cairn
#

if this is a problem with a specific crate, you should probably go to the github page and create an issue, or read the documentation more in-depth on docs.rs

topaz patrol
#

You're probably absolutely right 🙂

#

After giving this a rest and working on features, and after inevitably doing some refactoring, this 'issue' is now eliminated. This test was kind of a forceful experiment I shouldn't have lingered on 😉

#

So yeah, the above code was fully replaced with a new, much simpler, test.

#

This post might be distracting, so I feel it may be removed. I'm not a heavy Discord user (yet), and very new to this server, so I don't know if that is the best thing to do.