|
1 | 1 | message <<-MARKDOWN
|
2 |
| - Types of tests. |
| 2 | + Now, that you know what tests are, let's discuss the different types of tests! |
3 | 3 |
|
4 |
| - Bla bla bla. |
| 4 | + In your Rails app, you have models, views, and controllers -> is MVC ringing a bell? :) Well, it should be no surprise that tests can be written for models, views, and controllers. |
| 5 | + |
| 6 | + Take a look at the following Post model in your app: |
| 7 | + |
| 8 | + <div class="console"><pre> |
| 9 | + class Post < ActiveRecord::Base |
| 10 | + belongs_to :user |
| 11 | + has_many :replies |
| 12 | + validates :title, :user_id, :content, presence: true |
| 13 | + end |
| 14 | + </pre> |
| 15 | + </div> |
| 16 | + |
| 17 | + As you learned in the previous section, tests are used to verify that your code is working as expected. So, a couple things we can test right off the bat are that a post should have certain associations and validations. Let's start by writing some model tests also known as unit tests! |
| 18 | +MARKDOWN |
| 19 | + |
| 20 | +steps do |
| 21 | + step do |
| 22 | + message "First, create a post model spec file in the models folder of the spec folder. Type this in the terminal:" |
| 23 | + |
| 24 | + console_without_message "cd app/spec/models" |
| 25 | + console_without_message "touch post_spec.rb" |
| 26 | + end |
| 27 | + step do |
| 28 | + message "Then, run rspec." |
| 29 | + |
| 30 | + console_without_message "bundle exec rpsec" |
| 31 | + |
| 32 | + message "You should see some report but no tests exist yet. So, let's add one! Copy the below test, paste it into the post model spec file and then run 'bundle exec rspec' on the terminal again." |
| 33 | + |
| 34 | + console_without_message <<-RUBY |
| 35 | + describe 'ActiveRecord associations' do |
| 36 | + it 'Post belongs to users' do |
| 37 | + expect(Post.reflect_on_association(:user).macro).to be (:belongs_to) |
| 38 | + end |
| 39 | + end |
| 40 | + RUBY |
| 41 | + |
| 42 | + message "Great, now you should see one passing test! That's an example of an association test. Let's modify that test to fail. Then, run 'bundle exec rspec' and see what happens. Cool! Let's revert back to the passing test. And, write another association test for the relationship between the Post model and the Reply model!" |
| 43 | + |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | + |
| 48 | +message <<-MARKDOWN |
| 49 | + On to controller tests! Just like the Post model, assume we have the following controller in our app (the other methods are omitted for brevity): |
| 50 | + |
| 51 | + <div class="console"><pre> |
| 52 | + class PostsController < ApplicationController |
| 53 | + include PostsHelper |
| 54 | + |
| 55 | + def index |
| 56 | + @posts = Post.all |
| 57 | + render :index |
| 58 | + end |
| 59 | + |
| 60 | + def new |
| 61 | + @post = Post.new |
| 62 | + render :new |
| 63 | + end |
| 64 | + end |
| 65 | + </pre> |
| 66 | + </div> |
| 67 | +MARKDOWN |
| 68 | + |
| 69 | +steps do |
| 70 | + step do |
| 71 | + message "First, create a posts controller spec file in the controllers folder of the spec folder. Type this in the terminal:" |
| 72 | + |
| 73 | + console_without_message "cd app/spec/controllers" |
| 74 | + console_without_message "touch posts_controller_spec.rb" |
| 75 | + end |
| 76 | + step do |
| 77 | + message "Then, run rspec." |
| 78 | + |
| 79 | + console_without_message "bundle exec rpsec" |
| 80 | + |
| 81 | + message "You should see a report with some passing tests but those are just the model tests you wrote. So, let's add some controller tests! Copy the below test, paste it into the posts controller spec file and then run 'bundle exec rspec' on the terminal again." |
| 82 | + |
| 83 | + console_without_message <<-RUBY |
| 84 | + describe '#index' do |
| 85 | + it "renders the index view" do |
| 86 | + get :index |
| 87 | + expect(response).to render_template("index") |
| 88 | + end |
| 89 | + |
| 90 | + it "renders html" do |
| 91 | + process :index, method: :get |
| 92 | + expect(response.content_type).to eq "text/html" |
| 93 | + end |
| 94 | + end |
| 95 | + RUBY |
| 96 | + |
| 97 | + message "Great, that test should be passing! That's an example of a controller test on the index action. Let's modify that test to fail. Then, run 'bundle exec rspec' and see what happens. Awesome! Let's revert back to the passing test. Now, write another controller test for the new action (hint: you might need to look up what a mock is)." |
| 98 | + |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +message <<-MARKDOWN |
| 103 | + Last but not least: view tests! Below is an short snippet of the Post show HTML page in your app: |
5 | 104 | MARKDOWN
|
| 105 | +console_without_message <<-HTML |
| 106 | +<br> |
| 107 | +Post title: <%= @post.title %> |
| 108 | +<br> |
| 109 | +<br> |
| 110 | +Post content: <%= @post.content %> |
| 111 | +<br> |
| 112 | +HTML |
| 113 | + |
| 114 | +message <<-MARKDOWN |
| 115 | + So, based on the post you create, the show page should render HTML with the post's title and content. Let's verify that with a few tests. |
| 116 | +MARKDOWN |
| 117 | + |
| 118 | +steps do |
| 119 | + step do |
| 120 | + message "First, create a posts view spec file in the views folder of the spec folder. Type this in the terminal:" |
| 121 | + |
| 122 | + console_without_message "cd app/spec/views/posts" |
| 123 | + console_without_message "touch show.html.erb_spec.rb" |
| 124 | + end |
| 125 | + step do |
| 126 | + message "Then, run rspec." |
| 127 | + |
| 128 | + console_without_message "bundle exec rpsec" |
| 129 | + |
| 130 | + message "You should see a report with some passing tests but those are just the model and controller tests you wrote. So, let's add some view tests!" |
| 131 | + message "We're going to up the ante a bit here and NOT show you an example :) Google and StackOverflow are your friends here!" |
| 132 | + end |
| 133 | +end |
| 134 | + |
| 135 | +message <<-MARKDOWN |
| 136 | + Once, you have written some passing view tests, take a deep breath and pat yourself on the back! Testing is hard. But, it's critical in making sure software is stable and functional. |
| 137 | + |
| 138 | + So, I fibbed a little bit. There are more types of tests than just MVC tests. But, they're for another time. Just know about one more type of test called an integration test. As the name indicates, it tries to assess how well multiple components in an app interact and is written in Rails as a feature spec test. Typically, these tests simulate a user and a user's actions to test end-to-end functionality. |
| 139 | + |
| 140 | + Here's a diagram that may help with understanding how integration tests fit in: |
| 141 | + |
| 142 | +MARKDOWN |
| 143 | + |
| 144 | +img src: "img/rails-test-types.png", alt: "Thoughtbot's diagram of types of Rails tests" |
6 | 145 |
|
7 |
| -next_step "testing_frameworks" |
| 146 | +next_step "advanced_concepts" |
0 commit comments