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
#[fixture]
not returning the same instance when used by another fixture and a test?
#180
Comments
In order to have a static fixture you need to use But ....
You can look at #141 to see how to work around this.... Rust is not python and the things can sometime be little bit more complicated. |
I’m not talking about a static fixture. In pytest you get this behavior from the default function scope — a single test only creates one instance of the fixture, but with rstest I’m getting two instances. |
Sorry.... I've missed your point. You're right that's the expected behavior (the default Off course it is possible to implement something like this (I've already thought about it): a scoped once instance where you compute the object only if you don't already have one for the running asked scope, but that could not be the default behavior because in this case you cannot return the ownership of the fixture or a mutable reference to it but just a |
Ah right, I haven’t worked with Rust for a few months and didn’t think how ownership would make this hard… |
@la10736 thanks for creating this crate! I was also running into this just now.
That sounds like a solution indeed. |
(Sorry just browsing the repository and found this issue interesting) Rust newbie here, but would forcing the fixture to return a |
Fixtures are factory methods, and every time they are called generate a new instance. Of course the #[once] fixture is called once. One way to fix this is have a single fixture that generates a test context which all tests use, and has the proper stuff struct TestContext{
my_struct: MyStruct,
server: mockito::Server,
}
#[fixture]
async fn server() -> TestContext{
let server = mockito::Server::new_with_port_async(0).await;
let url = server.await.url();
TestContext {
server,
my_struct: MyStruct {
url
}
}
}
|
I have a sample project using mockito with rstest and it looks like a fixture used by another fixture and a test are receiving different instances of the object (in this case, the
server
fixture is used by themy_struct
fixture andtest_it
to get a URL, but the port differs between the two).Is this expected behavior? Coming from pytest, I would have expected only a single
mockito::Server
to be created fortest_it()
that's used by both the test and the fixture.Full example at https://github.com/rouge8/mockito-rstest
The text was updated successfully, but these errors were encountered: