Bob
Bob

Reputation: 848

Cake 3: Mock Behavior for Model

In Cake 3 I am testing a behavior. Now I want to test if the behavior is used nicely when saving via a model. For that I need to mock some methods of the behavior.

I have searched for many ways how to mock them and add them to the model. In Cake 2.x we were able to register a custom behavior via the ClassRegistry. I have searched for how to do that in Cake 3 but could not find it.

Any body knows how to do this?

Thanks

Upvotes: 1

Views: 655

Answers (1)

ndm
ndm

Reputation: 60463

You can simply add mocks manually to the tables behavior registry using ObjectRegistry::set(), which allows to add arbitrary objects.

$table = TableRegistry::get('FooBars');
$behaviorOptions = [];

$behaviorMock = $this->getMock(
    '\App\Model\Behavior\BazBehavior',
    ['some', 'mocked', 'methods'],
    [$table, $behaviorOptions]
);

$table->behaviors()->set('Baz', $behaviorMock);

Now your mocked behavior is registered on the table as Baz.

Upvotes: 1

Related Questions