Skip to content

Commit 13ead04

Browse files
committed
code examples legacy testing a singleton
1 parent 9876a2d commit 13ead04

2 files changed

Lines changed: 56 additions & 23 deletions

File tree

presentation/slides.md

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ https://chrisoldwood.blogspot.com/2016/11/tautologies-in-tests.html
566566

567567
---
568568
layout: code
569+
code-size: 1.2em
569570
h1:
570571
type: semicolon
571572
color: muted
@@ -687,15 +688,16 @@ test("calculateTotal_multipleItems_returnsSumOfPrices", () => {
687688
})
688689
```
689690

690-
<div class="mt-4 text-center text-lg">
691-
<div v-click.hide="1" class="text-orange-400"><strong>Method_Scenario_Expected</strong> — What are we testing?</div>
692-
<div v-click="[1,2]" class="text-emerald-400"><strong>Arrange</strong> — Set up the system under test</div>
693-
<div v-click="[2,3]" class="text-cyan-400"><strong>Act</strong> — Execute the behavior</div>
694-
<div v-click="[3,4]" class="text-pink-400"><strong>Assert</strong> — Verify the outcome</div>
691+
<div class="mt-4 text-center text-3xl grid">
692+
<div v-click.hide="1" class="text-orange-400 col-start-1 row-start-1"><strong>Method_Scenario_Expected</strong> — What are we testing?</div>
693+
<div v-click="[1,2]" class="text-emerald-400 col-start-1 row-start-1"><strong>Arrange</strong> — Set up the system under test</div>
694+
<div v-click="[2,3]" class="text-cyan-400 col-start-1 row-start-1"><strong>Act</strong> — Execute the behavior</div>
695+
<div v-click="[3,4]" class="text-pink-400 col-start-1 row-start-1"><strong>Assert</strong> — Verify the outcome</div>
695696
</div>
696697

697698
---
698-
layout: default
699+
layout: code
700+
code-size: 1em
699701
---
700702

701703
# Mocking in Practice
@@ -896,12 +898,13 @@ Seams: Change the behavior of a program without changing the program. Virtual me
896898
Sensing Variable: Introduce a variable that can be tested against.
897899
-->
898900

901+
899902
---
900903
layout: default
901-
h2:
902-
type: dot
903-
color: muted
904-
position: end
904+
h1:
905+
type: hash
906+
color: primary
907+
position: start
905908
---
906909

907910
# Legacy Code
@@ -915,30 +918,60 @@ h2:
915918
- Optionally create an interface
916919
- Service Locator
917920
- Register stubs to the IOC
921+
- Static Methods
922+
- Switch to ServiceLocator or Singleton
923+
- Or even better, switch to DI
918924

919925
</v-clicks>
920926

921-
<!-- Internal setter: InternalsVisibleTo assembly directive. -->
922927

923928
---
924-
layout: default
925-
h1:
926-
type: hash
927-
color: primary
928-
position: start
929+
layout: code-comparison
930+
before-label: Untestable
931+
after-label: Testable
929932
---
930933

931934
# Legacy Code
932935

933-
## How to test tricky code
936+
## How to test a Singleton
934937

935-
<v-clicks>
938+
::before::
936939

937-
- Static Methods
938-
- Switch to ServiceLocator or Singleton
939-
- Or even better, switch to DI
940+
```ts {2|6-8|all}
941+
class Logger {
942+
private static instance = new Logger()
943+
static getInstance() {
944+
return this.instance
945+
}
946+
log(msg: string) {
947+
/* writes to file */
948+
}
949+
}
950+
```
940951

941-
</v-clicks>
952+
::after::
953+
954+
<div v-click="2">
955+
956+
```ts {1-8|9-12|all}
957+
class Logger {
958+
private static instance = new Logger()
959+
static getInstance() {
960+
return this.instance
961+
}
962+
log(msg: string) {
963+
/* writes to file */
964+
}
965+
// 👇 Add seam for testing
966+
static setInstance(logger: Logger) {
967+
this.instance = logger
968+
}
969+
}
970+
```
971+
972+
</div>
973+
974+
<!-- Internal setter: InternalsVisibleTo assembly directive. Also consider extracting an interface for the Singleton. -->
942975

943976
---
944977
layout: section

0 commit comments

Comments
 (0)