Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
200
wkhtmltopdfの次どうするか問題2026
willnet
2
1.8k
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
480
一人だけ、Kiroが静止する日
hideg
0
130
setup-vp GitLab対応の裏側
naokihaba
0
120
新卒PdEのリアル
ryu1013
1
520
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
120
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
430
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
360
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
450
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
320
パズルゲームの作り方 / how to make puzzle games
kaityo256
PRO
2
240
Featured
See All Featured
Designing Powerful Visuals for Engaging Learning
tmiket
1
580
Context Engineering - Making Every Token Count
addyosmani
9
1.2k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
710
For a Future-Friendly Web
brad_frost
183
10k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Making the Leap to Tech Lead
cromwellryan
135
10k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Marketing to machines
jonoalderson
1
5.8k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
550
Why Our Code Smells
bkeepers
PRO
340
58k
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?