SlideShare uma empresa Scribd logo
1 de 63
Writing useful automated tests for
the Single Page Applications you
build
Andrei Sebastian Cîmpean, 2017
@Andrei_Cimpean
A talk about
things I do to
improve
feedback
Big JS Frameworks ~2017
➔ Work with a CLI
➔ Have a preferred testing
solution
➔ Maximize render performance
➔ Minimize boot time
➔ Components and
unidirectional data flow
➔ Things are directly coupled to
a multitude of things
➔ Any change to one member,
regardless of how trivial, will
likely cause many others to
require change
➔ Fragile codebase
Where gut feeling works
✔ Shitty code
Benefits that come with tests
➔Easier ramp-up for newcomers on the
project or feature
➔Code reuse is encouraged
➔Developer uses the code as a client would
Automated testing
➔ Isn’t always simple
➔ It's not always clear how to
break a problem into small,
testable units
➔ Code that is more dependent
on interaction than logic can
be hard to write tests for
➔ Testing is hard when tests are
slow
Every programmer knows
they should write tests for
their code. Few do.
Noel Rappin Journal 2
1. Writing automated tests
2. Writing automated tests for the ~2017 Single Page Apps
3. Writing useful automated tests for the ~2017 Single Page Apps
Writing useful automated tests for the Single Page Applications
you build
Types of tests ➔ That check state
➔ That check behaviorThere are other ways of separating tests
by type, but we’ll focus only on this one
State verification
Describes a style of testing where
you exercise one or many methods of
an object and then assert the
expected state of the object (and/or
collaborators).
Movie movie = a.movie.build();
Rental rental = a.rental.w(movie).build();
Store store = a.store.w(movie).build();
rental.start(store);
assertTrue(rental.isStarted());
assertEquals(
0, store.getAvailability(movie)
);
State verification tests specify the
least possible implementation
detail, thus they will continue to pass
even if the internals of the
methods being tested are changed.
Behavior verification
Describes a style of testing
where the execution of a
method is expected to
generate specific interactions
between objects.
Movie movie = a.movie.build();
Rental rental = a.rental.w(movie).build();
Store store = mock(Store.class);
when(store.getAvailability(movie))
.thenReturn(1);
rental.start(store);
assertTrue(rental.isStarted());
➔Behavior verification tests with minimal
collaborators can effectively verify
interactions without sacrificing
maintainability.
Law of Demeter
Only talk to your immediate friends
Tell, Don’t Ask
Rather than asking an object for data
and acting on that data, we should
instead tell an object what to do
The canonical
testing tools:
test doubles,
unit and
acceptance
tests
Test doubles
➔
Fake objects have incomplete/wrong working implementations
➔
Stubs provide canned answers to calls made during the test
➔
Spies are stubs that also record some information based on how they were
called
➔
Mocks are pre-programmed with expectations which form a specification of
the calls they are expected to receive.
https://martinfowler.com/bliki/TestDouble.html
Acceptance testing
➔
performed to verify that the product is acceptable to the customer and if it's
fulfilling the specified requirements
Unit testing
➔
it tests a unit of the program as a whole
Acceptance tests
➔ A test conducted to determine
if the requirements of a
specification or contract are
met
from http://gregbabiars.com/acceptance-testing-a-react-app-using-react-test-utils-pretender-and-rxjs/
Acceptance tests
➔ A test conducted to determine
if the requirements of a
specification or contract are
met
➔ Usually very slow to run
➔ There’s usually a strategy
put in place for when to
run them
Unit tests
➔ Low-level, focusing on a small
part of the software system
From http://learnrubythehardway.org/book/ex47.html
Unit tests
➔ Low-level, focusing on a small
part of the software system
➔ Significantly faster than
other kinds of tests
➔ Can be run very frequently
when programming
Do
Public methods are a contract.
APIs should be tested.
Do
It’s OK to write temporary tests for
private methods. Implementation
details is subject to change.
Enter
components
Components and unidirectional dataflow
➔ Data down - Actions up
➔ Presentational – Container
– Business vs. Toolkit
➔ Clear state management
Components make it obvious
what’s public and what’s private
➔ Props/Attributes are public
➔ Everything else is private
Unit test
According to the definition on wikipedia a unit can be an individual method,
but it can also be something much larger that likely includes many
collaborating classes.
Unit testing is an umbrella name for testing at a certain level of abstraction.
“Integration testing”
➔
between unit and acceptance testing
➔
test the rendered state of a component (or nested group of components) in
isolation from the rest of the app
Enter - Integration Testing
Integration tests
➔ How components behave in regard to
– DOM Changes
– Events
– Async
➔ Can be run very frequently when
programming
➔ Testing at the right level of
abstraction for SPAs
Integration tests are cheap today
➔ Modern JS frameworks
– Very fast rendering
– Fast boot
– “start fast, remain fast”
➔ Powerful machines
From https://www.mutuallyhuman.com/blog/2016/01/22/component-integration-testing-in-ember
Do
Focus on writing integration tests for
your components.
Do
Unit tests (for methods) are the
exception. Write them only when they
add substantially to confidence.
Templates change a lot
➔ Design changes
➔ Business
➔ Mistake
➔ Bug fixes
This will crash
even though the
logic didn’t.
Do
Avoid matching for class names in
integration tests.
Improve your setup!
➔ Automatically cleans the HTML of test
tags
➔ Ideally not only used in templates
– If you need to check private
attributes
➔ ember-test-selectors
– Or alternative for your framework
of choice
Template file
Test file
What about methods?
➔ Stop writing method tests that focus
on parameters
– If you can, and want to, use
Typescript or Flow
– Alternative, use inline assertions
➔ Stop writing method tests that focus
on parameters
– If you can, and want to, use
Typescript or Flow
– Alternative, use inline assertions
Ember.assert('Test for truthiness', obj);
Ember.assert('Fail unconditionally');
* In a production build, this method is defined as an empty
function (NOP). Uses of this method in Ember itself are
stripped from the ember.prod.js build
Inline asserts for rapid feedback
➔ Ember.assert
➔ Use it in appropriate places
– Lifecycle hooks when new
required properties are set
– Critical / fragile places
➔ “throw new Error”
– Clean up when building for
production
Do
Write integration tests that the
component is rendered correctly in all
the contexts.
Do
Write integration tests that all actions
are emitted correctly.
Things
developers
say
It’s not worth it and I’m testing my
code before I push to ...
It’s slowing me down...
It wasn’t done from the start of the
project...
The client doesn’t pay for it…
We have QA …
Things
developers
say
It’s not worth it and I’m
testing my code before I push
to ...
It’s slowing me down...
It wasn’t done from the start of the
project...
The client doesn’t pay for it…
We have QA ...
IT’S HARD
NOT SOMETHING I USUALLY
DO
Gut feeling and bias
Writing tests is slow
Not paid to write tests
You’re not paid to write tests, you just
write enough to be confident that
your code works as required.
Refactoring without having tests
is not refactoring, it’s moving code
around.
Not paid to write tests
“If I don’t typically make a kind of mistake (like setting the
wrong variables in a constructor), I don’t test for it. I do
tend to make sense of test errors, so I’m extra careful
when I have logic with complicated conditionals.
When coding on a team, I modify my strategy to carefully
test code that we, collectively, tend to get wrong.”
https://istacee.wordpress.com/2013/09/18/kent-beck-i-get-paid-for-code-that-works-not-for-tests/
You don't have
enough tests (or
good enough tests)
if you can't
confidently change
the code.
Do
Evaluate what you and your team get
out of tests and don’t be afraid to
tweak things around.
Do
Tests are part of the system and they
must be maintained to the same
standards as any other part of the
system.
TESTS
GIVE
CONFIDENYou don't have enough tests (or good enough tests)
if you can't confidently change the code.
If you have a lot of tests, a single
change to the production code can
cause hundreds of tests to require
corresponding changes.
For example, if you add an argument
to a method, every test that calls that
method must be changed to add the
new argument.
Fragile Test
Problem
SOLID PRINCIPLES
A one-to-one
correspondence
between
production and
test code implies
extremely tight
coupling.
Test and
production code
evolve in opposite
directions.
As the tests get
more specific, the
production code
gets more generic.
http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html?__s=5sgof5whdhfhhnfby3e4
A test suite that isn’t
run regularly doesn’t
have many
opportunities to
provide positive
ROI.
Do
Write a small test, make it pass.
Do
Write in small test/code cycles
Recap
➔ Write in small cycles
➔ Write integration tests for components
➔ Minimize DOM dependency in tests
➔ Only test what makes sense
➔ Refactor tests
➔ Don’t pretend you can refactor without
tests
Q & A
Would you rather throw away the code and keep the tests or vice-versa?
How do you know if your code-base is healthy?
“Mocks Aren’t Stubs”, Martin Fowler
“UnitTest”, Martin Fowler
“Is TDD dead?”, Kent Beck, David Heinemeier Hansson, Martin Fowler
“TDD Harms Architecture”, Rober C. Martin
"Working Effectively With Unit Tests", Jay Fields
“Test Driven Development”
Writing useful automated tests for the single page applications you build

Mais conteúdo relacionado

Mais procurados

Scrum and Test-driven development
Scrum and Test-driven developmentScrum and Test-driven development
Scrum and Test-driven development
toteb5
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Dhaval Dalal
 

Mais procurados (20)

VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Scrum and Test-driven development
Scrum and Test-driven developmentScrum and Test-driven development
Scrum and Test-driven development
 
TDD & BDD
TDD & BDDTDD & BDD
TDD & BDD
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD)
 
Test-Driven Development (TDD)
Test-Driven Development (TDD)Test-Driven Development (TDD)
Test-Driven Development (TDD)
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOS
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Acceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And FriendsAcceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And Friends
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
 
Common Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSCommon Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOS
 
Introduction to Acceptance Test Driven Development
Introduction to Acceptance Test Driven DevelopmentIntroduction to Acceptance Test Driven Development
Introduction to Acceptance Test Driven Development
 

Semelhante a Writing useful automated tests for the single page applications you build

Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
Richard Paul
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4
Billie Berzinskas
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
Ortus Solutions, Corp
 

Semelhante a Writing useful automated tests for the single page applications you build (20)

Software Testing
Software TestingSoftware Testing
Software Testing
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Testing recipes
Testing recipesTesting recipes
Testing recipes
 
Presentation
PresentationPresentation
Presentation
 
Devops e a nova cultura - TDC Florianopolis 2015
Devops e a nova cultura - TDC Florianopolis 2015Devops e a nova cultura - TDC Florianopolis 2015
Devops e a nova cultura - TDC Florianopolis 2015
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choice
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 
Making the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To TestingMaking the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To Testing
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Unit Test in Ruby on Rails by Minitest
Unit Test in Ruby on Rails by MinitestUnit Test in Ruby on Rails by Minitest
Unit Test in Ruby on Rails by Minitest
 
Beginners overview of automated testing with Rspec
Beginners overview of automated testing with RspecBeginners overview of automated testing with Rspec
Beginners overview of automated testing with Rspec
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 

Último

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 

Writing useful automated tests for the single page applications you build

  • 1.
  • 2. Writing useful automated tests for the Single Page Applications you build Andrei Sebastian Cîmpean, 2017 @Andrei_Cimpean
  • 3. A talk about things I do to improve feedback
  • 4. Big JS Frameworks ~2017 ➔ Work with a CLI ➔ Have a preferred testing solution ➔ Maximize render performance ➔ Minimize boot time ➔ Components and unidirectional data flow
  • 5. ➔ Things are directly coupled to a multitude of things ➔ Any change to one member, regardless of how trivial, will likely cause many others to require change ➔ Fragile codebase Where gut feeling works ✔ Shitty code
  • 6. Benefits that come with tests ➔Easier ramp-up for newcomers on the project or feature ➔Code reuse is encouraged ➔Developer uses the code as a client would
  • 7. Automated testing ➔ Isn’t always simple ➔ It's not always clear how to break a problem into small, testable units ➔ Code that is more dependent on interaction than logic can be hard to write tests for ➔ Testing is hard when tests are slow
  • 8. Every programmer knows they should write tests for their code. Few do. Noel Rappin Journal 2
  • 9. 1. Writing automated tests 2. Writing automated tests for the ~2017 Single Page Apps 3. Writing useful automated tests for the ~2017 Single Page Apps Writing useful automated tests for the Single Page Applications you build
  • 10. Types of tests ➔ That check state ➔ That check behaviorThere are other ways of separating tests by type, but we’ll focus only on this one
  • 11. State verification Describes a style of testing where you exercise one or many methods of an object and then assert the expected state of the object (and/or collaborators). Movie movie = a.movie.build(); Rental rental = a.rental.w(movie).build(); Store store = a.store.w(movie).build(); rental.start(store); assertTrue(rental.isStarted()); assertEquals( 0, store.getAvailability(movie) );
  • 12. State verification tests specify the least possible implementation detail, thus they will continue to pass even if the internals of the methods being tested are changed.
  • 13. Behavior verification Describes a style of testing where the execution of a method is expected to generate specific interactions between objects. Movie movie = a.movie.build(); Rental rental = a.rental.w(movie).build(); Store store = mock(Store.class); when(store.getAvailability(movie)) .thenReturn(1); rental.start(store); assertTrue(rental.isStarted());
  • 14. ➔Behavior verification tests with minimal collaborators can effectively verify interactions without sacrificing maintainability.
  • 15. Law of Demeter Only talk to your immediate friends Tell, Don’t Ask Rather than asking an object for data and acting on that data, we should instead tell an object what to do
  • 16. The canonical testing tools: test doubles, unit and acceptance tests
  • 17. Test doubles ➔ Fake objects have incomplete/wrong working implementations ➔ Stubs provide canned answers to calls made during the test ➔ Spies are stubs that also record some information based on how they were called ➔ Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive. https://martinfowler.com/bliki/TestDouble.html
  • 18. Acceptance testing ➔ performed to verify that the product is acceptable to the customer and if it's fulfilling the specified requirements Unit testing ➔ it tests a unit of the program as a whole
  • 19. Acceptance tests ➔ A test conducted to determine if the requirements of a specification or contract are met
  • 21.
  • 22. Acceptance tests ➔ A test conducted to determine if the requirements of a specification or contract are met ➔ Usually very slow to run ➔ There’s usually a strategy put in place for when to run them
  • 23. Unit tests ➔ Low-level, focusing on a small part of the software system
  • 25. Unit tests ➔ Low-level, focusing on a small part of the software system ➔ Significantly faster than other kinds of tests ➔ Can be run very frequently when programming
  • 26. Do Public methods are a contract. APIs should be tested. Do It’s OK to write temporary tests for private methods. Implementation details is subject to change.
  • 28. Components and unidirectional dataflow ➔ Data down - Actions up ➔ Presentational – Container – Business vs. Toolkit ➔ Clear state management
  • 29. Components make it obvious what’s public and what’s private ➔ Props/Attributes are public ➔ Everything else is private
  • 30. Unit test According to the definition on wikipedia a unit can be an individual method, but it can also be something much larger that likely includes many collaborating classes. Unit testing is an umbrella name for testing at a certain level of abstraction.
  • 31. “Integration testing” ➔ between unit and acceptance testing ➔ test the rendered state of a component (or nested group of components) in isolation from the rest of the app Enter - Integration Testing
  • 32. Integration tests ➔ How components behave in regard to – DOM Changes – Events – Async ➔ Can be run very frequently when programming ➔ Testing at the right level of abstraction for SPAs
  • 33. Integration tests are cheap today ➔ Modern JS frameworks – Very fast rendering – Fast boot – “start fast, remain fast” ➔ Powerful machines
  • 35. Do Focus on writing integration tests for your components. Do Unit tests (for methods) are the exception. Write them only when they add substantially to confidence.
  • 36. Templates change a lot ➔ Design changes ➔ Business ➔ Mistake ➔ Bug fixes This will crash even though the logic didn’t.
  • 37. Do Avoid matching for class names in integration tests.
  • 38. Improve your setup! ➔ Automatically cleans the HTML of test tags ➔ Ideally not only used in templates – If you need to check private attributes ➔ ember-test-selectors – Or alternative for your framework of choice
  • 41. What about methods? ➔ Stop writing method tests that focus on parameters – If you can, and want to, use Typescript or Flow – Alternative, use inline assertions ➔ Stop writing method tests that focus on parameters – If you can, and want to, use Typescript or Flow – Alternative, use inline assertions Ember.assert('Test for truthiness', obj); Ember.assert('Fail unconditionally'); * In a production build, this method is defined as an empty function (NOP). Uses of this method in Ember itself are stripped from the ember.prod.js build
  • 42. Inline asserts for rapid feedback ➔ Ember.assert ➔ Use it in appropriate places – Lifecycle hooks when new required properties are set – Critical / fragile places ➔ “throw new Error” – Clean up when building for production
  • 43. Do Write integration tests that the component is rendered correctly in all the contexts. Do Write integration tests that all actions are emitted correctly.
  • 44. Things developers say It’s not worth it and I’m testing my code before I push to ... It’s slowing me down... It wasn’t done from the start of the project... The client doesn’t pay for it… We have QA …
  • 45. Things developers say It’s not worth it and I’m testing my code before I push to ... It’s slowing me down... It wasn’t done from the start of the project... The client doesn’t pay for it… We have QA ... IT’S HARD NOT SOMETHING I USUALLY DO Gut feeling and bias
  • 47. Not paid to write tests You’re not paid to write tests, you just write enough to be confident that your code works as required. Refactoring without having tests is not refactoring, it’s moving code around.
  • 48. Not paid to write tests “If I don’t typically make a kind of mistake (like setting the wrong variables in a constructor), I don’t test for it. I do tend to make sense of test errors, so I’m extra careful when I have logic with complicated conditionals. When coding on a team, I modify my strategy to carefully test code that we, collectively, tend to get wrong.” https://istacee.wordpress.com/2013/09/18/kent-beck-i-get-paid-for-code-that-works-not-for-tests/
  • 49. You don't have enough tests (or good enough tests) if you can't confidently change the code.
  • 50. Do Evaluate what you and your team get out of tests and don’t be afraid to tweak things around.
  • 51. Do Tests are part of the system and they must be maintained to the same standards as any other part of the system.
  • 52. TESTS GIVE CONFIDENYou don't have enough tests (or good enough tests) if you can't confidently change the code.
  • 53. If you have a lot of tests, a single change to the production code can cause hundreds of tests to require corresponding changes. For example, if you add an argument to a method, every test that calls that method must be changed to add the new argument. Fragile Test Problem
  • 55. A one-to-one correspondence between production and test code implies extremely tight coupling.
  • 56. Test and production code evolve in opposite directions. As the tests get more specific, the production code gets more generic. http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html?__s=5sgof5whdhfhhnfby3e4
  • 57. A test suite that isn’t run regularly doesn’t have many opportunities to provide positive ROI.
  • 58. Do Write a small test, make it pass.
  • 59. Do Write in small test/code cycles
  • 60. Recap ➔ Write in small cycles ➔ Write integration tests for components ➔ Minimize DOM dependency in tests ➔ Only test what makes sense ➔ Refactor tests ➔ Don’t pretend you can refactor without tests
  • 61. Q & A Would you rather throw away the code and keep the tests or vice-versa? How do you know if your code-base is healthy?
  • 62. “Mocks Aren’t Stubs”, Martin Fowler “UnitTest”, Martin Fowler “Is TDD dead?”, Kent Beck, David Heinemeier Hansson, Martin Fowler “TDD Harms Architecture”, Rober C. Martin "Working Effectively With Unit Tests", Jay Fields “Test Driven Development”

Notas do Editor

  1. Intreabă cu ridicare de mână câţi sunt developer şi câţi sunt QA
  2. A team that relies on Behavior verification will likely produce a codebase with few Law of Demeter violations and a focus on Tell, Don’t Ask.
  3. Fragile Test Problem: As the number of tests grows, a single change to the production code can cause hundreds of tests to require corresponding changes (add an argument to a method, every test must be changed )
  4. If the structure of the tests follows the structure of the production code, then the tests are inextricably coupled to the production code
  5. These two streams of code evolve in opposite directions. Programmers refactor tests to become more and more concrete and specific. They refactor the production code to become more and more abstract and general. highly specific code cannot have a one-to-one correspondence with highly generic code