How to mock a node field value for phpunit? I'm working on a project where we need to have code coverage using phpunit.

I have a method that includes this code:

  $node = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;load($node_id);

  $form['data-title']['#value'] = $node-&gt;label();

  $form['data-description']['#value'] = $node-&gt;field_description-&gt;value;


So I need to mock out the node object.

I can get the node label to work, but not the field_description->value

Here's my set up function in my test class:

/**
   * {@inheritdoc}
   */
  protected function setUp() {
    $container = new Container();
    $container-&gt;set('string_translation', $this-&gt;getStringTranslationStub());

    $renderer = $this-&gt;getMockBuilder(Renderer::class)
      -&gt;disableOriginalConstructor()
      -&gt;getMock();
    $renderer
      -&gt;method('render')
      -&gt;willReturn('Hello world');

    $container-&gt;set('renderer', $renderer);

    // Mock entity type manager.
    $this-&gt;entityTypeManager = $this-&gt;getMockBuilder(EntityTypeManagerInterface::class)
      -&gt;disableOriginalConstructor()
      -&gt;getMock();

    // Mock a node and add the label to it.
    $node = $this-&gt;getMockBuilder(Node::class)
      -&gt;disableOriginalConstructor()
      -&gt;getMock();
    $node-&gt;expects($this-&gt;any())
      -&gt;method('label')
      -&gt;willReturn('shaken not stirred');

    $node-&gt;expects($this-&gt;any())
      -&gt;method('access')
      -&gt;willReturn(TRUE);

    $node-&gt;expects($this-&gt;any())
      -&gt;method('id')
      -&gt;willReturn(1);


    // @todo figure out how to add a field to a mock node.
    /*$field_description = new \stdClass();

    $field_description-&gt;value = 'This is a description';

    $node-&gt;set('field_description', $field_description);*/


    $node_storage = $this-&gt;getMockBuilder(EntityStorageInterface::class)
      -&gt;disableOriginalConstructor()
      -&gt;getMock();
    $node_storage-&gt;expects($this-&gt;any())
      -&gt;method('load')
      -&gt;willReturn($node);
    $this-&gt;entityTypeManager-&gt;expects($this-&gt;any())
      -&gt;method('getStorage')
      -&gt;willReturn($node_storage);

    $this-&gt;uuid = $this-&gt;getMockBuilder(UuidInterface::class)
      -&gt;disableOriginalConstructor()
      -&gt;getMock();

    $container-&gt;set('entity_type.manager', $this-&gt;entityTypeManager);

    $entity_repository = $this-&gt;getMockBuilder(EntityRepository::class)
      -&gt;disableOriginalConstructor()
      -&gt;getMock();
    $entity_repository-&gt;expects($this-&gt;any())
      -&gt;method('getTranslationFromContext')
      -&gt;willReturn($node);

    $container-&gt;set('entity.repository', $entity_repository);

    \Drupal::setContainer($container);
    $this-&gt;form = MyForm::create($container);

  }
The following should work using PHPUnit Mock Objects as in your example.

First mock FieldItemListInterface __get magic method.

$fieldDescMock = $this-&gt;getMockBuilder('\Drupal\Core\Field\FieldItemListInterface')
  -&gt;disableOriginalConstructor()
  -&gt;getMock();
$fieldDescMock-&gt;expects($this-&gt;any())
  -&gt;method('__get')
  -&gt;with('value')
  -&gt;willReturn('blah');


Then mock the Node/ContentEntityBase __get magic method.

$node-&gt;expects($this-&gt;any())
  -&gt;method('__get')
  -&gt;with('field_description')
  -&gt;willReturn($fieldDescMock);