Skip to content

feat: allow getting the Assert from an AssertError#144

Merged
epage merged 1 commit into
assert-rs:masterfrom
jbtrystram:assert_error
Oct 20, 2022
Merged

feat: allow getting the Assert from an AssertError#144
epage merged 1 commit into
assert-rs:masterfrom
jbtrystram:assert_error

Conversation

@jbtrystram

Copy link
Copy Markdown
Contributor

I'm in the process of writing integration tests with this awesome crate :)

My tool is basically a CRUD cli for a distant API, and while writing tests I ran into this problem while testing deletions.
In some cases, i need to check the output of the Assert even if it succeeded when I expected a failure ( but that would work the other way around.

An example : to verify a deletion process you'd do :delete, then read and expect an error :

    Command::cargo_bin("myTool")
        .unwrap()
        .arg("delete")
        .arg("resource")
		.assert()
		.success();

    Command::cargo_bin("myTool")
        .unwrap()
        .arg("read")
        .arg("resource")
		.assert()
		.failure();
}

However in some cases the "read" operation succeed because the resource was not yet deleted by the remote system.
I would need to check, because even if I was expecting a failure, a success might not invalidate the test.
Continuing the above example :

// first assert to delete the resource

// then read back: 

    let read = Command::cargo_bin("myTool")
        .unwrap()
        .arg("read")
        .arg("resource")
		.assert();

match read.try_failure() {
        Ok(assert) => {
				// here we are good but may want to verify a couple of things from the output
            	let output: &assert.get_output().stdout;
        }
        Err(err) => {
            let output: myType = serde_json::from_slice(&err.0.get_output().stdout).unwrap();
            // we check if it was marked for deletion. If so, it's all good.
            assert!(output.metadata.deletion_timestamp.is_some());
        }
    }

I was not sure if a getter method or simply marking the field public was the better choice there.. Let me know what you think.

@jbtrystram

Copy link
Copy Markdown
Contributor Author

@epage Bumping this. Would you be interested in that change ?

Comment thread src/assert.rs Outdated
@jbtrystram jbtrystram force-pushed the assert_error branch 2 times, most recently from ceb1876 to 36b6080 Compare October 18, 2022 07:27
@epage epage merged commit 7b0b08b into assert-rs:master Oct 20, 2022
@epage

epage commented Oct 20, 2022

Copy link
Copy Markdown
Contributor

This is now available in v2.0.5

takumi-earth pushed a commit to earthlings-dev/assert_cmd that referenced this pull request Jan 27, 2026
feat: allow getting the Assert from an AssertError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants