You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
84
+
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. Note: you may or may not need the Rspec in front of the describe depending on your Rspec version.
78
85
79
86
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:
Copy file name to clipboardExpand all lines: sites/en/testing/types_of_tests.step
+55-58Lines changed: 55 additions & 58 deletions
Original file line number
Diff line number
Diff line change
@@ -3,101 +3,87 @@ message <<-MARKDOWN
3
3
4
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
5
6
-
Create the Orange and Tree models in your app so that the model files are something like this:
6
+
Below, you will create the Orange model in your app so that the model file looks something like this:
7
7
8
-
<div class="console"><pre>
9
-
class Orange < ActiveRecord::Base
10
-
belongs_to :tree
11
-
validates :name, :tree_id, presence: true
12
-
end
13
-
14
-
class Tree < ActiveRecord::Base
15
-
has_many :oranges
16
-
validates :name, presence: true
17
-
end
18
-
</pre>
19
-
</div>
8
+
<div class="console"><pre>
9
+
class Orange < ActiveRecord::Base
10
+
belongs_to :tree
11
+
end
12
+
</pre>
13
+
</div>
20
14
21
-
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 tree should have certain associations and validations. Let's start by writing some model tests also known as unit tests!
15
+
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 tree should have certain associations and validations. Let's start by writing some model tests also known as unit tests! This link might come in handy to remember singular vs. plural Rails conventions: https://alexander-clark.com/blog/rails-conventions-singular-or-plural/
22
16
MARKDOWN
23
17
24
18
steps do
25
19
step do
26
-
message "First, create a orange model spec file in the models folder of the spec folder. Type this in the terminal:"
20
+
message "First, create a orange model. By creating the model, a spec file will also be added to the models folder of the spec folder. Type this in the terminal:"
21
+
22
+
console_without_message "rails g model Orange"
23
+
24
+
message "Then, run the migration to actually create the oranges table."
message "You should see some report but no tests exist yet. So, let's add one! Copy the below test, paste it into the orange model spec file and then run 'bundle exec rspec' on the terminal again."
37
34
38
35
console_without_message <<-RUBY
39
-
describe 'ActiveRecord associations' do
40
-
it 'Orange belongs to tree' do
41
-
expect(Orange.reflect_on_association(:tree).macro).to be (:belongs_to)
42
-
end
36
+
Rspec.describe Orange, :type => :model do
37
+
context 'ActiveRecord associations' do
38
+
it 'Orange belongs to tree' do
39
+
expect(Orange.reflect_on_association(:tree).macro).to be (:belongs_to)
40
+
end
41
+
end
43
42
end
44
43
RUBY
45
44
46
-
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 a has many association test for the relationship between the Tree model and the Orange model!"
45
+
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 a has many association test for the relationship between the Tree model (hint: this doesn't exist yet so you'll have to create the model and migrate!) and the Orange model!"
47
46
48
47
end
49
48
end
50
49
51
50
52
51
message <<-MARKDOWN
53
-
On to controller tests! Just like the Orange model, create the following controller in your app (the other methods are omitted for brevity):
54
-
55
-
<div class="console"><pre>
56
-
class OrangesController < ApplicationController
57
-
def index
58
-
@oranges = Orange.all
59
-
render :index
60
-
end
61
-
62
-
def new
63
-
@orange = Orange.new
64
-
render :new
65
-
end
66
-
end
67
-
</pre>
68
-
</div>
52
+
On to controller tests! Just like the Orange model, you will create the OrangesController, which will also create the spec files in the controller folder of the spec folder, in your app.
69
53
MARKDOWN
70
54
71
55
steps do
72
56
step do
73
-
message "First, create an orange controller spec file in the controllers folder of the spec folder. Type this in the terminal:"
57
+
message "First, create the oranges controller and relevant spec files by typing this in the terminal:"
console_without_message "rails g controller Oranges"
77
60
end
78
61
step do
79
62
message "Then, run rspec."
80
63
81
-
console_without_message "bundle exec rpsec"
64
+
console_without_message "bundle exec rspec"
82
65
83
-
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 oranges controller spec file and then run 'bundle exec rspec' on the terminal again."
66
+
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 oranges controller spec file, create the relevant view files and then run 'bundle exec rspec' on the terminal again."
84
67
85
68
console_without_message <<-RUBY
86
-
describe '#index' do
87
-
it "renders the index view" do
88
-
get :index
89
-
expect(response).to render_template("index")
90
-
end
91
-
92
-
it "renders html" do
93
-
process :index, method: :get
94
-
expect(response.content_type).to eq "text/html"
69
+
Rspec.describe OrangesController do
70
+
context '#index' do
71
+
it "renders the index view" do
72
+
get :index
73
+
expect(response).to render_template("index")
74
+
end
75
+
76
+
it "renders html" do
77
+
process :index, method: :get
78
+
expect(response.content_type).to eq "text/html"
79
+
end
95
80
end
96
81
end
97
82
RUBY
98
83
99
84
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)."
100
-
85
+
86
+
message "Note: as you write the controller tests, you may be prompted to install a missing gem called 'rails-controller-testing' to use the assert_template method. If prompted, please add it and do a bundle install!"
101
87
end
102
88
end
103
89
@@ -119,15 +105,17 @@ MARKDOWN
119
105
120
106
steps do
121
107
step do
122
-
message "First, create an oranges view spec file in the views folder of the spec folder. Type this in the terminal:"
108
+
message "First, create a views folder in the spec folder. Then, create an oranges folder in the views folder. Lastly, create an oranges view spec file in the oranges folder. Type these commands in the terminal:"
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!"
133
121
message "We're going to up the ante a bit here and NOT show you an example :) Google and StackOverflow are your friends here!"
@@ -137,12 +125,21 @@ end
137
125
message <<-MARKDOWN
138
126
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.
139
127
140
-
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.
128
+
So, I fibbed a little bit. There are more types of tests than just MVC tests. One of these other important tests is 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.
141
129
142
-
Here's a diagram that may help with understanding how integration tests fit in:
130
+
Here's a diagram from Thoughtbot that may help with understanding how integration tests fit in:
143
131
144
132
MARKDOWN
145
133
146
134
img src: "img/rails-test-types.png", alt: "Thoughtbot's diagram of types of Rails tests"
147
135
136
+
message <<-MARKDOWN
137
+
Capybara is a common framework used to write integration tests in Rails. It integrates nicely with RSpec such that you can use the same kind of test language to write integration tests.
138
+
139
+
As an added bonus for this section, write an integration test :)
140
+
141
+
Use the following link to get started: https://www.sitepoint.com/basics-capybara-improving-tests/
Copy file name to clipboardExpand all lines: sites/en/testing/what_are_tests.step
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Remember that we need to model an orange and a tree. So, we know that there are
10
10
11
11
We could say that the tree will not bear fruit until it matures at one year of age and then it will bear X number of oranges. We would need a test to test if the tree ages and a test to see if the tree has created X number of oranges once it has matured.
12
12
13
-
How do we determine if an orange is ripe? Well, we can have an orange age and if it is at least 30 days old, then it will fall from the tree. We would need to write a test to check for ripeness and a test to check if it falls at ripeness.
13
+
How do we determine if an orange is ripe? Well, we can have an orange age and if it is at least 30 days old, then it is ripe and will fall from the tree. We would need to write a test to check for ripeness and a test to check if it falls at ripeness.
14
14
15
15
# Why Is It Important That We Test?
16
16
You could think of tests as a requirement list. Every time a change is made we want to test to see if our code still meets those requirements. For example, as our program becomes increasingly complex and we want to have our tree to have a certain lifespan, would it make sense for our tree to continue to create oranges after exceeding that lifespan?
0 commit comments