I am creating a unit test for my simple method, this method get the value from requestHeader
here is my class which I want to create test class
namespace Vendor\Module\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Request\Http;
class Data extends AbstractHelper
{
const TEST_VALUE = 'KEYUR';
public function __construct(
Context $context,
Http $request
)
{
$this->request = $request;
parent::__construct($context);
}
// this is the method which I want to write a test case
public function getTestValue()
{
return $this->request->getHeader(self::TEST_VALUE);
}
}
and here is my test class,
namespace Vendor\Module\Test\Unit\Helper;
use Magento\Framework\App\Request\Http;
use Vendor\Module\Helper\Data;
use Magento\Framework\App\Helper\Context;
use Zend\Http\Header\HeaderInterface as HttpHeaderInterface;
class DataTest extends \PHPUnit\Framework\TestCase
{
protected $testValue = 'KEYUR';
/**
* @var requestHeaderMock|\PHPUnit_Framework_MockObject_MockObject
*/
protected $requestHeaderMock;
/**
* @var Data
*/
protected $myHelperMock;
/**
* @var HttpHeaderInterface
*/
protected $httpHeader;
protected function setUp()
{
$this->contextMock = $this->createMock(Context::class);
$this->requestMock = $this->createMock(Http::class);
$this->httpHeader = $this->createMock(HttpHeaderInterface::class);
$this->requestMock->method('getHeader')->willReturn($this->httpHeader);
$this->myHelperMock = new Data(
$this->contextMock,
$this->requestMock
);
}
public function testgetTestValue()
{
$this->requestMock
->expects($this->atLeastOnce())
->method('getHeader')
->with(Data::TEST_VALUE)
->willReturn($this->testValue);
$this->assertTrue($this->testValue == $this->myHelperMock->getTestValue());
}
}
But my test case fail with Failed asserting that false is true.
I do not know why ? Can someone put some shade on this ?