Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility of resolving a fixture only once per test? #263

Open
stevepryde opened this issue Jun 21, 2024 · 0 comments
Open

Possibility of resolving a fixture only once per test? #263

stevepryde opened this issue Jun 21, 2024 · 0 comments

Comments

@stevepryde
Copy link

This might be my misunderstanding of how fixtures are supposed to work, but I would love the ability to have fixtures resolved exactly once per test.

For example, I have a mock struct (let's call it MyMock) that contains the current mocked state.
In my tests I want to use this struct to add some things to the current test environment.

For example:

#[test]
fn test_thing() {
    let mock = MyMock::new();
    let foo1: Foo = mock.add_foo("foo1");
    let foo2: Foo = mock.add_foo("foo2");

    // Now run the function-to-be-tested, which also requires `&MyMock`
    let result = my_func(&mock).unwrap();
    assert_eq!(result, foo1); // I need foo1 here.
}

Can I use fixtures to get both MyMock as well as one or more Foos, created from the same instance of MyMock ?

For example, something like this?

#[fixture]
fn mock() -> MyMock {
    MyMock::new()
}

#[fixture]
fn foo(mock: &MyMock, #[default = "foo1"] name: &str) -> Foo {
    mock.add_foo(name)
}

#[rstest]
fn test_thing(mock: &MyMock, #[with("foo1")] #[from(foo)] foo1: Foo, #[with("foo2")] #[from(foo)] foo2: Foo) {
    let result = my_func(&mock).unwrap();
    assert_eq!(result, foo1);
}

Requirements:

  • I want a new instance of MyMock per test.
  • I want that instance to be shared by any fixtures used in the same test (and their fixture dependencies, and so on)

Is this possible?

--

(as an aside, it gets tedious to write this kind of thing:

#[with("foo1")] #[from(foo)] foo1: Foo, #[with("foo2")] #[from(foo)] foo2: Foo

is there any way to improve that?)

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

No branches or pull requests

1 participant