Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to RSpec

Intro to RSpec

Intro to RSpec
Dive into why we test, writing tests first vs after, and using Red / Green / Refactor to implement a small feature.

Stephanie Viccari

January 04, 2021
Tweet

More Decks by Stephanie Viccari

Other Decks in Education

Transcript

  1. Welcome! Let's get to know each other. ! introduce yourself

    ! share a fun fact OR something you're excited to learn in class ! Nominate someone to go next 2
  2. Before we get started... ! Join the Slack channel !

    Every question is important ! Tell us how it went! 4
  3. Class Schedule (PT AM) ! 9am - Class ☕ 10:30am

    - Break (10 min) ☕ 11:30am - Break (10 min) # 12:20pm - Fill out Survey (10 min) $ 12:30pm - Done! 5
  4. Class Schedule (PT PM) ! 1pm - Class ☕ 2:30pm

    - Break (10 min) ☕ 3:30pm - Break (10 min) # 4:20pm - Fill out Survey (10 min) $ 4:30pm - Done! 6
  5. Who is this class for? People who are: • new

    to writing tests • new to writing tests with RSpec • experienced and looking for new tips & tricks 7
  6. What We'll Cover in this Course • Introduction to Testing

    with RSpec • RSpec Matchers and Shared Examples • Mocks, Stubs, and Spies • Factories and let • Mitigating Flaky Tests • Testing Third-Party APIs • Testing Untested Code 8
  7. Open Discussion + Code Examples • "Ask Us Anything" Sessions

    • Examples for Testing Untested Code? 9
  8. Why Do We Write Tests? • Reduce bugs • Supply

    documentation • Improve the design of our code 10
  9. Test First Development Goal: Write a test that helps you

    make incremental progress Steps: 1. write a failing test 2. write code to make the test pass 3. refactor Also known as: - Test-Driven Development (TDD) - Red/Green/Refactor 12
  10. Test After Development Goal: Write tests for new/existing behavior Steps:

    1. write application code 2. write passing tests 3. refactor 13
  11. Test First Development ! Benefits • Validate or invalidate assumptions

    • Encourages refactoring • Influences the design of our code 14
  12. Test First Development ! Costs • Takes practice • Requires

    writing a test when we know the least about the problem 15
  13. Test First Development How can I "test first" when untested

    code already exists? 1) Write tests for existing behavior. * (ex: test the Happy Path) 2) Write tests for new behavior. 16
  14. Test After Development ! Benefits • Avoid writing unnecessary tests

    (e.g., Technical or Feature Spike) • Writing tests when you know more about the problem 17
  15. Test After Development ! Costs • Slower feedback due to

    manual testing • Discourages incremental refactoring • Fewer warnings that our code is hard to test 18
  16. Terminology • Unit tests ⭐ • Model tests • Controller

    tests • View tests • Feature tests • Regression tests • End-to-End tests • Component tests • Contract tests • Integration tests • UI tests • System tests 19
  17. Unit Tests Test code in isolation, independent of the surrounding

    system. Sample Application: Examples: • View spec • Model spec 20
  18. Integration Tests Exercise a subset of the system to ensure

    separate pieces of code work together. Sample Application: Examples: • Controller specs • Job specs • Models specs 22
  19. End-to-End Tests Exercise the system as a whole, typically written

    from the user's perspective. Sample Application: Examples: • Feature spec 23
  20. Let's Code! Goal: Use the Red, Green, Refactor cycle to

    implement a calculator that can: • add two numbers • calculate factorials Exercise Cadence: I'll start us off. Then we'll mob program the rest, borrowing from the "Who Wants to be a Millionaire" Q&A style. 26
  21. Review We used Red, Green, Refactor to: • design on

    our code's interface • guide us with helpful error messages • refactor safely • commit working code with confidence 27
  22. Organizing our Tests RSpec.describe MyClass do describe "used to describe

    behavior" do it "describes a specific example" do; end context "highlights a special circumstance" do it "describes a specific example" do; end end end end 28
  23. Four Phase Test RSpec.describe Calculator do describe "#add" do it

    "returns the sum of the two arguments" do setup (required setup) exercise (exercise system under test) verify (result is verified against an expectation) teardown (system under test is reset) end end end 29
  24. RSpec Focus Filter RSpec version <= 3.4 RSpec.configure do |config|

    config.filter_run_including :focus => true config.run_all_when_everything_filtered = true end 30
  25. RSpec Focus Filter RSpec version >= 3.5 RSpec.configure do |config|

    config.filter_run_when_matching: :focus end 31
  26. Resources Martin Fowler TestPyramid - https://martinfowler.com/bliki/TestPyramid.html RSpec.describe vs describe -

    https://relishapp.com/rspec/rspec-core/v/3-7/docs/ configuration/zero-monkey-patching-mode Four-Phase Test - https://thoughtbot.com/blog/four-phase-test RSpec Focus Filter v3.4 - https://relishapp.com/rspec/rspec-core/v/3-4/docs/filtering/ inclusion-filters - https://relishapp.com/rspec/rspec-core/v/3-4/docs/ configuration/run-all-when-everything-filtered RSpec Focus Filter v3.5 - https://relishapp.com/rspec/rspec-core/v/3-5/docs/filtering/ filter-run-when-matching 32