Attribute Macro cucumber_rust_codegen::when[][src]

#[when]
Expand description

Attribute to auto-wire the test to the World implementer.

There are 3 step-specific attributes:

  • [given]
  • [when]
  • [then]

Example

use cucumber_rust::{given, World, WorldInit};

#[derive(WorldInit)]
struct MyWorld;

#[async_trait(?Send)]
impl World for MyWorld {
    type Error = Infallible;

    async fn new() -> Result<Self, Self::Error> {
        Ok(Self {})
    }
}

#[given(regex = r"(\S+) is (\d+)")]
fn test(w: &mut MyWorld, param: String, num: i32) {
    assert_eq!(param, "foo");
    assert_eq!(num, 0);
}

#[tokio::main]
async fn main() {
    let runner = MyWorld::init(&["./features"]);
    runner.run().await;
}

Arguments

  • First argument has to be mutable refence to the WorldInit deriver (your World implementer).
  • Other argument’s types have to implement FromStr or it has to be a slice where the element type also implements FromStr.
  • To use cucumber::StepContext, name the argument as context, or mark the argument with a #[given(context)] attribute.
#[derive(WorldInit)]
struct MyWorld;

#[given(regex = r"(\S+) is not (\S+)")]
fn test_step(
    w: &mut MyWorld,
    #[given(context)] s: &StepContext,
) {
    assert_eq!(s.matches.get(0).unwrap(), "foo");
    assert_eq!(s.matches.get(1).unwrap(), "bar");
    assert_eq!(s.step.value, "foo is bar");
}