Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Mockery & mocking basics
Search
Jan Škrášek
February 28, 2015
Programming
160
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Mockery & mocking basics
Source code:
https://github.com/hrach/posobota-mockery
Jan Škrášek
February 28, 2015
Other Decks in Programming
See All in Programming
引き算の組織 ― アウトカムとAIに全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
280
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
110
Gmail/Google DriveをトリガーにAIエージェントを動かそう! / Run AI agents with Gmail/Google Drive as triggers!
har1101
2
430
ソフトウェアラスタライザ
fadis
1
530
【QA Test Talk Vol.8】AI-DLC による Whole Team Approach の加速
pkshadeck
PRO
0
220
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
350
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
320
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
360
Oxlintはいいぞ(続)
yug1224
1
400
AIに既存システムを理解させる技術 ~レガシーを見捨てないハーネスエンジニアリング入門~
ochtum
0
140
不幸な GC
chencmd
0
660
レビュー履歴をAIに食わせて、 Compose移行を加速するs
shihochan
0
200
Featured
See All Featured
Color Theory Basics | Prateek | Gurzu
gurzu
0
430
Joys of Absence: A Defence of Solitary Play
codingconduct
1
440
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
810
Building AI with AI
inesmontani
PRO
1
1.2k
Chasing Engaging Ingredients in Design
codingconduct
0
280
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Optimising Largest Contentful Paint
csswizardry
37
3.9k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
380
The Invisible Side of Design
smashingmag
301
52k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
490
Transcript
Mockery & mocking basics Jan Škrášek @hrachcz github.com/nextras github.com/hrach
Poll • Who is testing? • Who use mocking tool?
• Who use Mockery?
Test types • Unit tests • Ingtegrations tests o Regression
tests • Acceptance tests • … Mock objects
Integration tests • object composition • real functionality
Unit tests • class functionality only! • all dependencies mocked
Mock object Mock object is simulated object that mimic the
behavior of real object in a controlled way.
Integration test $driver = $connection->getDriver(); $parser = new SqlProcessor($driver); Assert::same(
'SELECT `a`.`b`', $parser->process('SELECT [a.b]') );
Unit test $driver = Mockery::mock(IDriver::class); $parser = new SqlProcessor($driver); $driver->shouldReceive('delimite')
->with('a.b')->andReturn('`a`.`b`'); Assert::same( 'SELECT `a`.`b`', $parser->process('SELECT [a.b]') );
Mockery Composer mockery/mockery Pádraic Brady Current version 0.9
Mockery – general $mock = Mockery::mock(IDriver::class); // $mock instanceof MockInterface;
$mock->should… Mockery::close();
Mockery – methods $mock->shouldRecieve('method') ->once()->twice() ->times(4) ->ordered() ->with('foo') ->andReturn('bar')->andThrow()
Mockery – arguments $mock->shouldRecieve('method') ->with(Mockery::any()) ->with(2, Mockery::any()) ->with(Mockery::on(function($arg) { return
TRUE; }) ->with(Mockery::anyOf(1, 2)) ->with(Mockery::type('float')
Mockery – returning $mock->shouldRecieve('method') ->andReturn(TRUE) ->andReturnUsing(function($arg) { return strtoupper($arg); })
->andThrow() ->andSet($property, $value);
Unit tests • Are needed • Are useful • But…
there are some exceptions…
$this->mapper->shouldReceive('getRepository')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getEntityClassNames')->once()->andReturn(['EntityClass']); $this->metadataStorage->shouldReceive('get')->once()->with('EntityClass')->andReturn($this->entityM $this->queryBuilder->shouldReceive('getFromAlias')->once()->andReturn('books'); $this->mapper->shouldReceive('getStorageReflection')->once()->andReturn($this->reflection); $propertyMetadata = Mockery::mock('Nextras\Orm\Entity\Reflection\PropertyMetadata'); $propertyMetadata->relationshipRepository =
'AuthorsRepository'; $propertyMetadata->relationshipType = PropertyMetadata::RELATIONSHIP_MANY_HAS_ONE; // translator $this->entityMetadata->shouldReceive('getProperty')->once()->with('translator')->andReturn($property $this->model->shouldReceive('getRepository')->once()->with('AuthorsRepository')->andReturn($this-> $this->model->shouldReceive('getMapper')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getStorageReflection')->once()->andReturn($this->reflection); $this->reflection->shouldReceive('getStoragePrimaryKey')->once()->andReturn(['id']); $this->reflection->shouldReceive('convertEntityToStorageKey')->once()->with('translator')->andReturn( $this->mapper->shouldReceive('getTableName')->once()->andReturn('authors'); $this->mapper->shouldReceive('getRepository')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getEntityClassNames')->once()->andReturn(['EntityClass2']); $this->metadataStorage->shouldReceive('get')->once()->with('EntityClass2')->andReturn($this->entityM // name $this->entityMetadata->shouldReceive('getProperty')->once()->with('name'); $this->reflection->shouldReceive('convertEntityToStorageKey')->once()->with('name')->andReturn('nam $this->queryBuilder->shouldReceive('leftJoin')->once()->with('books', 'authors', 'authors', '[books.transl $this->queryBuilder->shouldReceive('addOrderBy')->once()->with('authors.name'); $this->builderHelper->processOrderByExpression('this->translator->name', ICollection::ASC, $this->qu
Unit tests • To much work to mock? o Just
SPLIT the class. • Should we mock?: o scalars/arrays/objects o entities/crates o model layer • Integration tests
Integration tests • Are quite difficult • You need: o
Setup database/queue o Prepare testing data (insert into database) o Working internet connection o … • Solution: PARTIAL MOCKS
Partial mocks • Mock only something • Mock only some
methods $mock = Mockery::mock(MysqlDriver::class) ->makePartial(); $mock->shouldReceive(…);
DEMO TIME
Recap • Use composition!!! • Use interfaces. • Create small
classes. • Choose your library carefully. • Write unit/integration tests.
Thank you Jan Škrášek @hrachcz github.com/nextras github.com/hrach Questions?