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
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
610
気持ちぃ〜角丸 Squircle
ryu1013
0
310
APNsからLive Activityを開始する話
yumnumm
0
170
Railsバージョンを4倍にした話
optim
0
440
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
210
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
180
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
5
5.1k
ChromeでローカルLLM
ryu1013
0
140
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
180
golang-bob.pdf
iwatsukayura
0
130
Domain-Driven Transformation
hschwentner
2
2.3k
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
160
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
37
7.5k
How to Ace a Technical Interview
jacobian
280
24k
Intergalactic Javascript Robots from Outer Space
tanoku
273
28k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
780
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
560
Being A Developer After 40
akosma
92
590k
Product Roadmaps are Hard
iamctodd
55
13k
Building Better People: How to give real-time feedback that sticks.
wjessup
371
20k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.4k
Building AI with AI
inesmontani
PRO
1
1.3k
Mind Mapping
helmedeiros
1
400
Crafting Experiences
bethany
1
380
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?