问题
How can we force the locale used by Dusk within our tests?
The browser is launched with FR locale set and thus my application is outputting French.
I would need to make all tests consistently, regardless of the locale set by the developer on his system when running tests.
In the middleware stack, I have a LocaleMiddleware
responsible for picking a locale based on the request headers (sent automatically by the browser).
If possible, I would like to avoid having test-specific code within the application code (as suggested below, to force a locale when environment is 'testing')
回答1:
Use setLocale()
method:
app()->setLocale('en');
Or:
App::setLocale('en');
You may also change the active language at runtime using the
setLocale
method
https://laravel.com/docs/5.5/localization#introduction
Update
You can also set locale in the middleware for testing environment:
if (env('APP_ENV') === 'testing') {
app()->setLocale('en');
return $next($request);
}
来源:https://stackoverflow.com/questions/46835334/forcing-locale-in-laravel-dusk