|
1 | 1 | message <<-MARKDOWN
|
2 |
| - Testing frameworks. |
| 2 | +# What's a testing framework? |
| 3 | +A Testing framework is an execution environment for automated tests. Think of it as the set of assumptions that reminds you when you veer away from those assumptions. In short Test frameworks helps teams organize their test suites and in turn |
| 4 | +help improve the efficiency of testing. |
| 5 | + |
| 6 | +# Types of testing frameworks |
| 7 | +There are many testing frameworks that work great. Mini Test is the default testing framework in Rails 5. However, we will be using the RSpec testing framework instead. |
| 8 | + |
| 9 | + |
| 10 | +# RSpec |
| 11 | +## How to set up RSpec in Rails |
| 12 | + |
| 13 | +Add rspec-rails to both the :development and :test groups in the Gemfile: |
| 14 | + |
| 15 | +<div class="console"><pre> |
| 16 | +group :development, :test do |
| 17 | + gem 'rspec-rails', '~> 3.4' |
| 18 | +end</pre> |
| 19 | +</div> |
| 20 | + |
| 21 | +Download and install by running: |
| 22 | + |
| 23 | +<div class="console"><pre> |
| 24 | +bundle install |
| 25 | +</pre> |
| 26 | +</div> |
| 27 | + |
| 28 | +Initialize the spec/ directory (where specs will reside) with: |
| 29 | + |
| 30 | +<div class="console"><pre> |
| 31 | +rails generate rspec:install |
| 32 | +</pre> |
| 33 | +</div> |
| 34 | + |
| 35 | +This adds the following files which are used for configuration: |
| 36 | + |
| 37 | +<div class="console"><pre> |
| 38 | +.rspec |
| 39 | +spec/spec_helper.rb |
| 40 | +spec/rails_helper.rb |
| 41 | +</pre> |
| 42 | +</div> |
| 43 | + |
| 44 | +Use the rspec command to run your specs: |
| 45 | + |
| 46 | +<div class="console"><pre> |
| 47 | +bundle exec rspec |
| 48 | +</pre> |
| 49 | +</div> |
| 50 | + |
| 51 | +By default the above will run all spec files in the spec directory. |
| 52 | + |
| 53 | +To run only a subset of these specs use the following command: |
| 54 | + |
| 55 | +<div class="console"><pre> |
| 56 | +# Run only a specific folder name |
| 57 | +bundle exec rspec spec/folder_name |
| 58 | + |
| 59 | +# Run only specs for a specific type of test such as the post controller |
| 60 | +bundle exec rspec spec/controllers/post_controller_spec.rb |
| 61 | +</pre> |
| 62 | +</div> |
| 63 | + |
| 64 | +## RSpec Basics |
| 65 | + |
| 66 | +<div class="console"><pre> |
| 67 | +1 RSpec.describe Tree do |
| 68 | +2 it "is able to age by 1 year increments" do |
| 69 | +3 orange_tree = Tree.new |
| 70 | +4 orange_tree.age |
| 71 | +5 expect(orange_tree.age).to eq(1) |
| 72 | +6 end |
| 73 | +7 end |
| 74 | +</pre> |
| 75 | +</div> |
| 76 | + |
| 77 | +The 'describe' and 'it' methods come from rspec-core. The Tree class would be from your code. You can think of 'describe' as a header to describe which class you are testing and 'it' as a string/subheader that states what specifically you are testing in the Tree class. |
| 78 | + |
| 79 | +The last line of the example expresses an expected outcome. If orange_tree.age == 1, then the example passes. If not, it fails with a message like: |
| 80 | + |
| 81 | +<div class="console"> |
| 82 | +<pre> |
| 83 | +expected: #< Tree @age=1 > |
| 84 | + got: #< Tree @age=0 > |
| 85 | +</pre> |
| 86 | +</div> |
| 87 | + |
| 88 | +## Matchers |
| 89 | +Remember in our example on line 5? 'to eq' is a matcher. RSpec has many built-in matchers. Matchers evaluates our expectations. In our example, we are saying that we expect orange_tree's age to equal an integer of 1. |
| 90 | + |
| 91 | +Check out the other built-in matchers! |
| 92 | +https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers |
| 93 | + |
3 | 94 |
|
4 |
| - Bla bla bla. |
5 | 95 | MARKDOWN
|
6 | 96 |
|
7 | 97 | next_step "types_of_tests"
|
0 commit comments