How to add an entity reference field value on Behat Test My Behat test starts like this:

@api
Scenario: Testing user
Given users:
    | name | pass | mail    | status | roles  | field_lines_ref |
    | test | test | t@t.com | 1      | Tester | Line Name       |


field_lines_ref is an entity reference to a node.  The user gets created but the Lines Reference field never gets populated.  I also tried using the node id but it also didn't work.

One thing to note is that I already have a node created "Line Name" and I have to use that exact node because of logic in place (Based on node id).
If you have an entity reference field, you need to refer to the entity with it's name/title/label field (this applies for taxonomy terms too). Like you did it.

This code will run for your association:

/**
 * Entityreference field handler for Drupal 7.
 */
class EntityreferenceHandler extends AbstractHandler {

  /**
   * {@inheritdoc}
   */
  public function expand($values) {
    $entity_type = $this-&gt;fieldInfo['settings']['target_type'];
    $entity_info = entity_get_info($entity_type);
    // For users set label to username.
    if ($entity_type == 'user') {
      $entity_info['entity keys']['label'] = 'name';
    }

    $return = array();
    foreach ($values as $value) {
      $target_id = db_select($entity_info['base table'], 't')
        -&gt;fields('t', array($entity_info['entity keys']['id']))
        -&gt;condition('t.' . $entity_info['entity keys']['label'], $value)
        -&gt;execute()-&gt;fetchField();
      if ($target_id) {
        $return[$this-&gt;language][] = array('target_id' =&gt; $target_id);
      }
    }
    return $return;
  }

}


Which also means, the first item will be tried to associate with your user, which doesn't mean, that it can be (i.e. for content type restriction). So make sure you have only one entity which named like you want to associate. Also make sure, you don't have ', ' and ' - ' in your value, because those will be converted to multi value, and if ' - ' is present than the ': ' value will be converted to key: value pairs. You need to say exactly the entity name which is possible to refer inside that field (but as far as I can see in the code, make sure you don't have space before and/or after the name field's value, because the query above may fail to find your entity). Also check if your field is a real field, (not like the path 'field', which is not a field) you can do it, be executing the field_info_field_map() command, and if you find your field machine name there, as a key, you are fine.

If all the above is fulfilled and you already has a node which can be referred by the user's field_lines_ref field and the node title is 'Line Name' you should be able to create your user with the reference:

Given users:
    | name | pass | mail    | status | roles  | field_lines_ref |
    | test | test | t@t.com | 1      | Tester | Line Name       |


From features file or

$user_obj = (object) array(
  'name' =&gt; 'test',
  'pass' =&gt; 'test',
  'mail' =&gt; 't@t.com',
  'status' =&gt; '1',
  'roles' =&gt; 'Tester',
  'field_lines_ref' =&gt; 'Line Name',
);
$user = $this-&gt;userCreate($user_obj);


from context object.

If these doesn't help, you may need to debug your own code (including vendor code), to see where is the problem.