diff --git a/src/Tests/DynamicEntityReferenceFieldTest.php b/src/Tests/DynamicEntityReferenceBaseFieldTest.php
similarity index 79%
copy from src/Tests/DynamicEntityReferenceFieldTest.php
copy to src/Tests/DynamicEntityReferenceBaseFieldTest.php
index 72be5f7..96e6685 100644
--- a/src/Tests/DynamicEntityReferenceFieldTest.php
+++ b/src/Tests/DynamicEntityReferenceBaseFieldTest.php
@@ -2,15 +2,13 @@
 
 /**
  * @file
- * Contains \Drupal\dynamic_entity_reference\Tests\DynamicEntityReferenceFieldTest.
+ * Contains \Drupal\dynamic_entity_reference\Tests\DynamicEntityReferenceBaseFieldTest.
  */
 
 namespace Drupal\dynamic_entity_reference\Tests;
 
 use Drupal\config\Tests\SchemaCheckTestTrait;
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
-use Drupal\field\Entity\FieldConfig;
-use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\system\Tests\Entity\EntityUnitTestBase;
 
 /**
@@ -18,7 +16,7 @@ use Drupal\system\Tests\Entity\EntityUnitTestBase;
  *
  * @group dynamic_entity_reference
  */
-class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
+class DynamicEntityReferenceBaseFieldTest extends EntityUnitTestBase {
   use SchemaCheckTestTrait;
 
   /**
@@ -33,7 +31,7 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
    *
    * @var string
    */
-  protected $referencedEntityType = 'entity_test_rev';
+  protected $referencedEntityType = 'entity_test_mul';
 
   /**
    * The bundle used in this test.
@@ -47,51 +45,24 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
    *
    * @var string
    */
-  protected $fieldName = 'field_test';
+  protected $fieldName = 'dynamic_references';
 
   /**
    * Modules to install.
    *
    * @var array
    */
-  public static $modules = array('dynamic_entity_reference');
-
-  /**
-   * {@inheritdoc}
-   */
-  protected function setUp() {
-    parent::setUp();
-
-    $this->installEntitySchema('entity_test_rev');
-
-    // Create a field.
-    FieldStorageConfig::create(array(
-      'field_name' => $this->fieldName,
-      'type' => 'dynamic_entity_reference',
-      'entity_type' => $this->entityType,
-      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
-      'settings' => array(
-        'exclude_entity_types' => FALSE,
-        'entity_type_ids' => [
-          $this->referencedEntityType,
-        ],
-      ),
-    ))->save();
-
-    FieldConfig::create(array(
-      'field_name' => $this->fieldName,
-      'entity_type' => $this->entityType,
-      'bundle' => $this->bundle,
-      'label' => 'Field test',
-      'settings' => array(),
-    ))->save();
-
-  }
+  public static $modules = ['dynamic_entity_reference'];
 
   /**
    * Tests reference field validation.
    */
   public function testEntityReferenceFieldValidation() {
+    \Drupal::state()->set('dynamic_entity_reference_cardinality', 1);
+    \Drupal::state()->set('dynamic_entity_reference_entity_test_exclude', 'entity_test');
+    $this->enableModules(['dynamic_entity_reference_entity_test']);
+    \Drupal::entityDefinitionUpdateManager()->applyUpdates();
+    $this->installEntitySchema('entity_test_mul');
     $entity_manager = \Drupal::entityManager();
     // Test a valid reference.
     $referenced_entity = $entity_manager
@@ -107,12 +78,17 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 0, 'Validation passes.');
 
-    $entity = entity_create($this->entityType, array('type' => $this->bundle));
+    $entity = $entity_manager
+      ->getStorage($this->entityType)
+      ->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->entity = $referenced_entity;
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 0, 'Validation passes.');
 
     // Test an invalid reference.
+    $entity = $entity_manager
+      ->getStorage($this->entityType)
+      ->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->target_type = $referenced_entity->getEntityTypeId();
     $entity->{$this->fieldName}->target_id = 9999;
     $violations = $entity->{$this->fieldName}->validate();
@@ -120,6 +96,9 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
     $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', array('%type' => $this->referencedEntityType, '%id' => 9999)));
 
     // Test an invalid target_type.
+    $entity = $entity_manager
+      ->getStorage($this->entityType)
+      ->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->target_type = $entity->getEntityTypeId();
     $entity->{$this->fieldName}->target_id = $referenced_entity->id();
     $violations = $entity->{$this->fieldName}->validate();
@@ -127,6 +106,9 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
     $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', array('%type' => $this->entityType, '%id' => $referenced_entity->id())));
 
     // Test an invalid entity.
+    $entity = $entity_manager
+      ->getStorage($this->entityType)
+      ->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->entity = $entity;
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 1, 'Validation throws a violation.');
@@ -140,6 +122,11 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
    * Tests the multiple target entities loader.
    */
   public function testReferencedEntitiesMultipleLoad() {
+    \Drupal::state()->set('dynamic_entity_reference_cardinality', FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
+    \Drupal::state()->set('dynamic_entity_reference_entity_test_exclude', 'entity_test');
+    $this->enableModules(['dynamic_entity_reference_entity_test']);
+    \Drupal::entityDefinitionUpdateManager()->applyUpdates();
+    $this->installEntitySchema('entity_test_mul');
     $entity_manager = \Drupal::entityManager();
     // Create the parent entity.
     $entity = $entity_manager
@@ -174,10 +161,10 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
     $reference_field[6]['target_type'] = NULL;
     $target_entities[6] = NULL;
 
-    // Attach the first created target entity as the eighth item ($delta == 7)
-    // of the parent entity field. We want to test the case when the same target
-    // entity is referenced twice (or more times) in the same dynamic entity
-    // reference field.
+    // Attach the first created target entity as the sixth item ($delta == 5) of
+    // the parent entity field. We want to test the case when the same target
+    // entity is referenced twice (or more times) in the same entity reference
+    // field.
     $reference_field[7] = $reference_field[0];
     $target_entities[7] = $target_entities[0];
 
diff --git a/src/Tests/DynamicEntityReferenceBaseTest.php b/src/Tests/DynamicEntityReferenceBaseTest.php
new file mode 100644
index 0000000..456f916
--- /dev/null
+++ b/src/Tests/DynamicEntityReferenceBaseTest.php
@@ -0,0 +1,365 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\dynamic_entity_reference\Tests\DynamicEntityReferenceBaseTest.
+ */
+
+namespace Drupal\dynamic_entity_reference\Tests;
+
+use Drupal\Component\Utility\Crypt;
+use Drupal\Component\Utility\Unicode;
+use Drupal\Core\Field\BaseFieldDefinition;
+use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\Core\Language\LanguageInterface;
+use Drupal\Core\Site\Settings;
+use Drupal\Core\Url;
+use Drupal\entity_test\Entity\EntityTest;
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
+use Drupal\simpletest\WebTestBase;
+use Drupal\taxonomy\Entity\Term;
+use Drupal\taxonomy\Entity\Vocabulary;
+use Symfony\Component\CssSelector\CssSelector;
+
+/**
+ * Ensures that Dynamic Entity References field works correctly.
+ *
+ * @group dynamic_entity_reference
+ */
+class DynamicEntityReferenceBaseTest extends WebTestBase {
+
+  /**
+   * Admin user
+   *
+   * @var \Drupal\Core\Session\AccountInterface
+   */
+  protected $adminUser;
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = array(
+    'field_ui',
+    'dynamic_entity_reference',
+    'entity_test',
+  );
+
+  /**
+   * Permissions to grant admin user.
+   *
+   * @var array
+   */
+  protected $permissions = array(
+    'access administration pages',
+    'view test entity',
+    'administer entity_test fields',
+    'administer entity_test content',
+  );
+
+  /**
+   * Sets the test up.
+   */
+  protected function setUp() {
+    parent::setUp();
+    $this->adminUser = $this->drupalCreateUser($this->permissions);
+  }
+
+  /**
+   * Tests adding and editing single values using dynamic entity reference.
+   */
+  public function testSingleValueDynamicEntityReference() {
+    \Drupal::state()->set('dynamic_entity_reference_cardinality', 1);
+    \Drupal::service('module_installer')->install(['dynamic_entity_reference_entity_test']);
+    $this->drupalLogin($this->adminUser);
+
+    // Create some items to reference.
+    $item1 = EntityTest::create([
+      'name' => 'item1',
+    ]);
+    $item1->save();
+
+    // Test the new entity commenting inherits default.
+    $this->drupalGet('entity_test/add');
+    $this->assertField('dynamic_references[0][target_id]', 'Found foobar field target id');
+    $this->assertField('dynamic_references[0][target_type]', 'Found foobar field target type');
+
+    // Ensure that the autocomplete path is correct.
+    $input = $this->xpath('//input[@name=:name]', array(':name' => 'dynamic_references[0][target_id]'))[0];
+    ;
+    $settings = \Drupal::entityManager()->getBaseFieldDefinitions('entity_test')['dynamic_references']->getSettings();
+    $selection_settings = $settings['entity_test']['handler_settings'] ?: [];
+    $data = serialize($selection_settings) . 'entity_test' . $settings['entity_test']['handler'];
+    $selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());
+    $expected_autocomplete_path = Url::fromRoute('system.entity_autocomplete', array(
+      'target_type' => 'entity_test',
+      'selection_handler' => $settings['entity_test']['handler'],
+      'selection_settings_key' => $selection_settings_key,
+    ))->toString();
+    $this->assertTrue(strpos((string) $input['data-autocomplete-path'], $expected_autocomplete_path) !== FALSE);
+
+    $edit = array(
+      // Ensure that an exact match on a unique label is accepted.
+      'dynamic_references[0][target_id]' => 'item1',
+      'dynamic_references[0][target_type]' => 'entity_test',
+      'name[0][value]' => 'Barfoo',
+      'user_id[0][target_id]' => $this->adminUser->label() . ' (' . $this->adminUser->id() . ')',
+    );
+
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+    $entities = \Drupal::entityManager()
+      ->getStorage('entity_test')
+      ->loadByProperties(array(
+        'name' => 'Barfoo',
+      ));
+    $this->assertEqual(1, count($entities), 'Entity was saved');
+    $entity = reset($entities);
+    $this->drupalGet('entity_test/' . $entity->id());
+    $this->assertText('Barfoo');
+    $this->assertText('item1');
+
+    $this->assertEqual(count($entity->dynamic_references), 1, 'One item in field');
+    $this->assertEqual($entity->dynamic_references[0]->entity->label(), 'item1');
+
+    $this->drupalGet('entity_test/manage/' . $entity->id());
+
+
+    $edit = array(
+      'name[0][value]' => 'Bazbar',
+      // Remove one child.
+      'dynamic_references[0][target_id]' => '',
+    );
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+    $this->drupalGet('entity_test/' . $entity->id());
+    $this->assertText('Bazbar');
+    // Reload entity.
+    \Drupal::entityManager()->getStorage('entity_test')->resetCache(array($entity->id()));
+    $entity = EntityTest::load($entity->id());
+    $this->assertTrue($entity->dynamic_references->isEmpty() , 'No value in field');
+
+    // Create two entities with the same label.
+    $labels = array();
+    $duplicates = array();
+    for ($i = 0; $i < 2; $i++) {
+      $duplicates[$i] = EntityTest::create([
+        'name' => 'duplicate label',
+      ]);
+      $duplicates[$i]->save();
+      $labels[$i] = $duplicates[$i]->label() . ' (' . $duplicates[$i]->id() . ')';
+    }
+
+    // Now try to submit and just specify the label.
+    $this->drupalGet('entity_test/manage/' . $entity->id());
+    $edit = array(
+      'dynamic_references[0][target_id]' => 'duplicate label',
+    );
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+
+    // We don't know the order in which the entities will be listed, so just
+    // assert parts and make sure both are shown.
+    $error_message = t('Multiple entities match this reference;');
+    $this->assertRaw($error_message);
+    $this->assertRaw($labels[0]);
+    $this->assertRaw($labels[1]);
+
+    // Create a few more to trigger the case where there are more than 5
+    // matching results.
+    for ($i = 2; $i < 7; $i++) {
+      $duplicates[$i] = EntityTest::create([
+        'name' => 'duplicate label',
+      ]);
+      $duplicates[$i]->save();
+      $labels[$i] = $duplicates[$i]->label() . ' (' . $duplicates[$i]->id() . ')';
+    }
+
+    // Submit again with the same values.
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+
+    $params = array(
+      '%value' => 'duplicate label',
+    );
+    // We don't know which id it will display, so just assert a part of the
+    // error.
+    $error_message = t('Many entities are called %value. Specify the one you want by appending the id in parentheses', $params);
+    $this->assertRaw($error_message);
+
+    // Submit with a label that does not match anything.
+    // Now try to submit and just specify the label.
+    $this->drupalGet('entity_test/manage/' . $entity->id());
+    $edit = array(
+      'dynamic_references[0][target_id]' => 'does not exist',
+    );
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+    $this->assertRaw(t('There are no entities matching "%value".', array('%value' => 'does not exist')));
+
+    $this->drupalGet('entity_test/manage/' . $entity->id());
+    $edit = array(
+      'name[0][value]' => 'Bazbar',
+      // Reference itself.
+      'dynamic_references[0][target_id]' => 'Bazbar (' . $entity->id() . ')',
+    );
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+    $this->drupalGet('entity_test/' . $entity->id());
+    $this->assertText('Bazbar');
+    // Reload entity.
+    \Drupal::entityManager()->getStorage('entity_test')->resetCache(array($entity->id()));
+    $entity = EntityTest::load($entity->id());
+    $this->assertEqual($entity->dynamic_references[0]->entity->label(), 'Bazbar');
+  }
+
+  /**
+   * Tests adding and editing multi values using dynamic entity reference.
+   */
+  public function testMultiValueDynamicEntityReference() {
+    \Drupal::state()->set('dynamic_entity_reference_cardinality', -1);
+    \Drupal::service('module_installer')->install(['dynamic_entity_reference_entity_test']);
+    $this->drupalLogin($this->adminUser);
+
+    // Create some items to reference.
+    $item1 = EntityTest::create([
+      'name' => 'item1',
+    ]);
+    $item1->save();
+    $item2 = EntityTest::create([
+      'name' => 'item2',
+    ]);
+    $item2->save();
+
+    // Test the new entity commenting inherits default.
+    $this->drupalGet('entity_test/add');
+    $this->assertField('dynamic_references[0][target_id]', 'Found foobar field target id');
+    $this->assertField('dynamic_references[0][target_type]', 'Found foobar field target type');
+
+    // Ensure that the autocomplete path is correct.
+    $input = $this->xpath('//input[@name=:name]', array(':name' => 'dynamic_references[0][target_id]'))[0];
+    ;
+    $settings = \Drupal::entityManager()->getBaseFieldDefinitions('entity_test')['dynamic_references']->getSettings();
+    $selection_settings = $settings['entity_test']['handler_settings'] ?: [];
+    $data = serialize($selection_settings) . 'entity_test' . $settings['entity_test']['handler'];
+    $selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());
+    $expected_autocomplete_path = Url::fromRoute('system.entity_autocomplete', array(
+      'target_type' => 'entity_test',
+      'selection_handler' => $settings['entity_test']['handler'],
+      'selection_settings_key' => $selection_settings_key,
+    ))->toString();
+    $this->assertTrue(strpos((string) $input['data-autocomplete-path'], $expected_autocomplete_path) !== FALSE);
+
+    // Add some extra dynamic entity reference fields.
+    $this->drupalPostAjaxForm(NULL, array(), array('dynamic_references_add_more' => t('Add another item')), NULL, array(), array(), 'entity-test-entity-test-form');
+
+    $edit = array(
+      // Ensure that an exact match on a unique label is accepted.
+      'dynamic_references[0][target_id]' => 'item1',
+      'dynamic_references[0][target_type]' => 'entity_test',
+      'dynamic_references[1][target_id]' => 'item2 (' . $item2->id() . ')',
+      'dynamic_references[1][target_type]' => 'entity_test',
+      'name[0][value]' => 'Barfoo',
+      'user_id[0][target_id]' => $this->adminUser->label() . ' (' . $this->adminUser->id() . ')',
+    );
+
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+    $entities = \Drupal::entityManager()
+      ->getStorage('entity_test')
+      ->loadByProperties(array(
+      'name' => 'Barfoo',
+    ));
+    $this->assertEqual(1, count($entities), 'Entity was saved');
+    $entity = reset($entities);
+    $this->drupalGet('entity_test/' . $entity->id());
+    $this->assertText('Barfoo');
+    $this->assertText('item1');
+    $this->assertText('item2');
+
+    $this->assertEqual(count($entity->dynamic_references), 2, 'Two items in field');
+    $this->assertEqual($entity->dynamic_references[0]->entity->label(), 'item1');
+    $this->assertEqual($entity->dynamic_references[1]->entity->label(), 'item2');
+
+    $this->drupalGet('entity_test/manage/' . $entity->id());
+
+
+    $edit = array(
+      'name[0][value]' => 'Bazbar',
+      // Remove one child.
+      'dynamic_references[1][target_id]' => '',
+    );
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+    $this->drupalGet('entity_test/' . $entity->id());
+    $this->assertText('Bazbar');
+    // Reload entity.
+    \Drupal::entityManager()->getStorage('entity_test')->resetCache(array($entity->id()));
+    $entity = EntityTest::load($entity->id());
+    $this->assertEqual(count($entity->dynamic_references), 1, 'One value in field');
+
+    // Create two entities with the same label.
+    $labels = array();
+    $duplicates = array();
+    for ($i = 0; $i < 2; $i++) {
+      $duplicates[$i] = EntityTest::create([
+        'name' => 'duplicate label',
+      ]);
+      $duplicates[$i]->save();
+      $labels[$i] = $duplicates[$i]->label() . ' (' . $duplicates[$i]->id() . ')';
+    }
+
+    // Now try to submit and just specify the label.
+    $this->drupalGet('entity_test/manage/' . $entity->id());
+    $edit = array(
+      'dynamic_references[1][target_id]' => 'duplicate label',
+    );
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+
+    // We don't know the order in which the entities will be listed, so just
+    // assert parts and make sure both are shown.
+    $error_message = t('Multiple entities match this reference;');
+    $this->assertRaw($error_message);
+    $this->assertRaw($labels[0]);
+    $this->assertRaw($labels[1]);
+
+    // Create a few more to trigger the case where there are more than 5
+    // matching results.
+    for ($i = 2; $i < 7; $i++) {
+      $duplicates[$i] = EntityTest::create([
+        'name' => 'duplicate label',
+      ]);
+      $duplicates[$i]->save();
+      $labels[$i] = $duplicates[$i]->label() . ' (' . $duplicates[$i]->id() . ')';
+    }
+
+    // Submit again with the same values.
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+
+    $params = array(
+      '%value' => 'duplicate label',
+    );
+    // We don't know which id it will display, so just assert a part of the
+    // error.
+    $error_message = t('Many entities are called %value. Specify the one you want by appending the id in parentheses', $params);
+    $this->assertRaw($error_message);
+
+    // Submit with a label that does not match anything.
+    // Now try to submit and just specify the label.
+    $this->drupalGet('entity_test/manage/' . $entity->id());
+    $edit = array(
+      'dynamic_references[1][target_id]' => 'does not exist',
+    );
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+    $this->assertRaw(t('There are no entities matching "%value".', array('%value' => 'does not exist')));
+
+    $this->drupalGet('entity_test/manage/' . $entity->id());
+    $edit = array(
+      'name[0][value]' => 'Bazbar',
+      // Reference itself.
+      'dynamic_references[1][target_id]' => 'Bazbar (' . $entity->id() . ')',
+    );
+    $this->drupalPostForm(NULL, $edit, t('Save'));
+    $this->drupalGet('entity_test/' . $entity->id());
+    $this->assertText('Bazbar');
+    // Reload entity.
+    \Drupal::entityManager()->getStorage('entity_test')->resetCache(array($entity->id()));
+    $entity = EntityTest::load($entity->id());
+    $this->assertEqual($entity->dynamic_references[1]->entity->label(), 'Bazbar');
+  }
+
+}
diff --git a/src/Tests/DynamicEntityReferenceFieldTest.php b/src/Tests/DynamicEntityReferenceFieldTest.php
index 72be5f7..0f2de35 100644
--- a/src/Tests/DynamicEntityReferenceFieldTest.php
+++ b/src/Tests/DynamicEntityReferenceFieldTest.php
@@ -107,12 +107,17 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 0, 'Validation passes.');
 
-    $entity = entity_create($this->entityType, array('type' => $this->bundle));
+    $entity = $entity_manager
+      ->getStorage($this->entityType)
+      ->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->entity = $referenced_entity;
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 0, 'Validation passes.');
 
     // Test an invalid reference.
+    $entity = $entity_manager
+      ->getStorage($this->entityType)
+      ->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->target_type = $referenced_entity->getEntityTypeId();
     $entity->{$this->fieldName}->target_id = 9999;
     $violations = $entity->{$this->fieldName}->validate();
@@ -120,6 +125,9 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
     $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', array('%type' => $this->referencedEntityType, '%id' => 9999)));
 
     // Test an invalid target_type.
+    $entity = $entity_manager
+      ->getStorage($this->entityType)
+      ->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->target_type = $entity->getEntityTypeId();
     $entity->{$this->fieldName}->target_id = $referenced_entity->id();
     $violations = $entity->{$this->fieldName}->validate();
@@ -127,6 +135,9 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase {
     $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', array('%type' => $this->entityType, '%id' => $referenced_entity->id())));
 
     // Test an invalid entity.
+    $entity = $entity_manager
+      ->getStorage($this->entityType)
+      ->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->entity = $entity;
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 1, 'Validation throws a violation.');
diff --git a/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.info.yml b/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.info.yml
new file mode 100644
index 0000000..740ad89
--- /dev/null
+++ b/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.info.yml
@@ -0,0 +1,8 @@
+name: 'Dynamic Entity Reference Entity Test'
+type: module
+description: 'Provides base fields for dynamic_entity_reference tests of entity_test entity type.'
+package: Testing
+core: 8.x
+dependencies:
+  - dynamic_entity_reference
+  - entity_test
diff --git a/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module b/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module
new file mode 100644
index 0000000..ab81d3c
--- /dev/null
+++ b/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module
@@ -0,0 +1,70 @@
+<?php
+
+/**
+ * @file
+ * Module file for dynamic_entity_reference_entity_test.
+ */
+
+use Drupal\Core\Entity\EntityTypeInterface;
+use Drupal\Core\Field\BaseFieldDefinition;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
+
+/**
+ * Implements hook_entity_base_field_info().
+ */
+function dynamic_entity_reference_entity_test_entity_base_field_info(EntityTypeInterface $entity_type) {
+  $fields = [];
+  // 'entity_test' with no data table and 'entity_test_mul' is with data table.
+  if ($entity_type->id() == 'entity_test' || $entity_type->id() == 'entity_test_mul') {
+    $fields['dynamic_references'] = BaseFieldDefinition::create('dynamic_entity_reference')
+      ->setLabel((string) new TranslatableMarkup('References'))
+      ->setDescription((string) new TranslatableMarkup('Reference another entity.'))
+      ->setRequired(FALSE)
+      ->setCardinality(\Drupal::state()->get('dynamic_entity_reference_cardinality', 1))
+      ->setDisplayOptions('form', [
+        'type' => 'dynamic_entity_reference_default',
+        'weight' => 10,
+      ])
+      ->setDisplayOptions('view', [
+        'label' => 'above',
+        'type' => 'dynamic_entity_reference_label',
+        'weight' => 10,
+      ])
+      ->setDisplayConfigurable('form', TRUE)
+      ->setDisplayConfigurable('view', TRUE);
+    if (\Drupal::state()->get('dynamic_entity_reference_entity_test_exclude', '') == 'entity_test') {
+      $fields['dynamic_references']
+        ->setSettings([
+          'exclude_entity_types' => FALSE,
+          'entity_type_ids' => [
+            'entity_test_mul' => 'entity_test_mul',
+          ],
+          'entity_test_mul' => [
+            'handler' => "default:entity_test_mul",
+            'handler_settings' => [],
+          ],
+        ]);
+    }
+    else {
+      $fields['dynamic_references']
+      ->setSettings([
+        'exclude_entity_types' => FALSE,
+        'entity_type_ids' => [
+          'entity_test' => 'entity_test',
+          'entity_test_mul' => 'entity_test_mul',
+        ],
+        'entity_test' => [
+          'handler' => "default:entity_test",
+          'handler_settings' => [],
+        ],
+        'entity_test_mul' => [
+          'handler' => "default:entity_test_mul",
+          'handler_settings' => [],
+        ],
+      ]);
+    }
+
+  }
+
+  return $fields;
+}
