Rebuild drupal_container().
@todo Fix http://drupal.org/node/1708692 so that module enable/disable changes are immediately reflected in drupal_container(). Until then, tests can invoke this workaround when requiring services from newly enabled modules to be immediately available in the same request.
protected function rebuildContainer() {
// Create a new DrupalKernel for testing purposes, now that all required
// modules have been enabled. This also stores a new dependency injection
// container in drupal_container(). Drupal\simpletest\TestBase::tearDown()
// restores the original container.
// @see Drupal\Core\DrupalKernel::initializeContainer()
$this->kernel = new DrupalKernel('testing', FALSE, drupal_classloader(), FALSE);
// Booting the kernel is necessary to initialize the new DIC. While
// normally the kernel gets booted on demand in
// Symfony\Component\HttpKernel\handle(), this kernel needs manual booting
// as it is not used to handle a request.
$this->kernel
->boot();
// The DrupalKernel does not update the container in drupal_container(), but
// replaces it with a new object. We therefore need to replace the minimal
// boostrap container that has been set up by TestBase::prepareEnvironment().
$this->container = drupal_container();
}