You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently there's no built-in way to access the components actually used by the running application for a given test, and there's not really any guidance on how to do so manually. You can use components, but that will create new instances rather than using the existing one. It doesn't make sense to use the injector of the current application because it's not populated for compile-time DI apps. See also playframework/playframework#8099.
One way to solve the problem is to use a trait like this to satisfy the components dependency:
traitWithComponents[C<:BuiltInComponents] extendsWithApplicationComponents {
privatevar_components:C=null// Override this to customize how new components are created.defnewComponents:C// This method is used to access the current components inside a test.finaldefcurrentComponents:C=if (_components eq null) sys.error("Components not defined!") else _components
// This method is called once when creating an application.// Use currentComponents to access the components inside a test.finaldefcomponents:BuiltInComponents= {
_components = newComponents
_components
}
}
While it's possible to do this yourself, it doesn't feel very obvious so I think it would be useful to add a helper like this to the library, or change the WithApplicationComponents to work like this.
Part of the problem is also that we're using variables at the class level to store the application, rather than doing all this in a local scope. This also means it doesn't make sense to run tests in the same class in parallel, because they share this state. So we may also want to recommend a local test scope for each test rather than having it automatically provided by the testing traits, something like WithApplication in play-specs2.
The text was updated successfully, but these errors were encountered:
Currently there's no built-in way to access the components actually used by the running application for a given test, and there's not really any guidance on how to do so manually. You can use
components
, but that will create new instances rather than using the existing one. It doesn't make sense to use theinjector
of the current application because it's not populated for compile-time DI apps. See also playframework/playframework#8099.One way to solve the problem is to use a trait like this to satisfy the
components
dependency:While it's possible to do this yourself, it doesn't feel very obvious so I think it would be useful to add a helper like this to the library, or change the
WithApplicationComponents
to work like this.Part of the problem is also that we're using variables at the class level to store the application, rather than doing all this in a local scope. This also means it doesn't make sense to run tests in the same class in parallel, because they share this state. So we may also want to recommend a local test scope for each test rather than having it automatically provided by the testing traits, something like WithApplication in play-specs2.
The text was updated successfully, but these errors were encountered: