Yii Framework Forum: Using Codeception Stub Class For Unit Tests - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Using Codeception Stub Class For Unit Tests Making Best Use Of Codeception Features

#1 User is offline   The Peach 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 08-October 09
  • Location:London, UK

Posted 24 September 2014 - 01:35 PM

Hi all,
back again on testing.
I'm still stuck on a couple of things regarding unit testing.
I'm trying to see if it's possible to use CodeCeption Stub static class.

Let's take as an example Stubbing the security component.
What I need to do is just have the two methods validatePassword and generatePasswordHash to return what I need.
With CodeCeption it seems I should be able to do the following:

$security = Stub::construct(
    'yii\base\Security',
    [
        'validatePassword' => true,
        'generatePasswordHash' => $expectedPassword
    ]
);
Yii::$app->set('security', $security);


which seems to be really neat and clean.
Unfortunately this doesn't work and throws all kind of errors.

Instead the PHPUnit classic method works flawlessly:

$security = $this->getMock(
    'yii\base\Security',
    ['validatePassword', 'generatePasswordHash']
);
$security->expects($this->any())
    ->method('validatePassword')
    ->with($expectedPassword)
    ->willReturn(true);
$security->expects($this->any())
    ->method('generatePasswordHash')
    ->with($expectedPassword)
    ->willReturn($expectedPassword);


Any ideas here?
0

#2 User is offline   The Peach 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 08-October 09
  • Location:London, UK

Posted 04 October 2014 - 05:02 AM

There's no solution for this, e.g. use PHPUnit mocking functionality.

For more in depth discussion and links head over to:
https://github.com/y...ii2/issues/5210
0

#3 User is offline   Borner 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 08-August 16

Posted 15 January 2018 - 09:22 AM

View PostThe Peach, on 04 October 2014 - 05:02 AM, said:

There's no solution for this, e.g. use PHPUnit mocking functionality.

For more in depth discussion and links head over to:
https://github.com/y...ii2/issues/5210


Does it mean, it's not possible to mock data for Yii2 codeception test?

Currently I try to get an Yii2/codeception unittest running and i like to load different data into an model (from an array / mock /...) in order to test the $model->verify() function.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users