Skip to content

Commit 0a8e8c6

Browse files
Akansh Murthytjgrathwell
authored andcommitted
update with class feedback
1 parent 5387595 commit 0a8e8c6

File tree

3 files changed

+66
-62
lines changed

3 files changed

+66
-62
lines changed

sites/en/testing/testing_frameworks.step

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ There are many testing frameworks that work great. Mini Test is the default tes
1010
# RSpec
1111
## How to set up RSpec in Rails
1212

13-
First, create a new Rails app. Then, add rspec-rails to both the :development and :test groups in the Gemfile:
13+
First, create a new Rails app.
14+
15+
<div class="console"><pre>
16+
rails new testapp
17+
</pre>
18+
</div>
19+
20+
Then, add rspec-rails to both the :development and :test groups in the Gemfile:
1421

1522
<div class="console"><pre>
1623
group :development, :test do
@@ -64,7 +71,7 @@ bundle exec rspec spec/controllers/post_controller_spec.rb
6471
## RSpec Basics
6572

6673
<div class="console"><pre>
67-
1 describe Tree do
74+
1 Rspec.describe Tree do
6875
2 it "is able to age by 1 year increments" do
6976
3 orange_tree = Tree.new
7077
4 orange_tree.age
@@ -74,7 +81,7 @@ bundle exec rspec spec/controllers/post_controller_spec.rb
7481
</pre>
7582
</div>
7683

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.
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.
7885

7986
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:
8087

sites/en/testing/types_of_tests.step

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,87 @@ message <<-MARKDOWN
33

44
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.
55

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:
77

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>
2014

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/
2216
MARKDOWN
2317

2418
steps do
2519
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."
2725

28-
console_without_message "cd app/spec/models"
29-
console_without_message "touch orange_spec.rb"
26+
console_without_message "bundle exec rake db:migrate"
3027
end
3128
step do
3229
message "Then, run rspec."
3330

34-
console_without_message "bundle exec rpsec"
31+
console_without_message "bundle exec rspec"
3532

3633
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."
3734

3835
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
4342
end
4443
RUBY
4544

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!"
4746

4847
end
4948
end
5049

5150

5251
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.
6953
MARKDOWN
7054

7155
steps do
7256
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:"
7458

75-
console_without_message "cd app/spec/controllers"
76-
console_without_message "touch oranges_controller_spec.rb"
59+
console_without_message "rails g controller Oranges"
7760
end
7861
step do
7962
message "Then, run rspec."
8063

81-
console_without_message "bundle exec rpsec"
64+
console_without_message "bundle exec rspec"
8265

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."
8467

8568
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
9580
end
9681
end
9782
RUBY
9883

9984
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!"
10187
end
10288
end
10389

@@ -119,15 +105,17 @@ MARKDOWN
119105

120106
steps do
121107
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:"
123109

110+
console_without_message "mkdir app/spec/views"
111+
console_without_message "mkdir app/spec/views/oranges"
124112
console_without_message "cd app/spec/views/oranges"
125113
console_without_message "touch show.html.erb_spec.rb"
126114
end
127115
step do
128116
message "Then, run rspec."
129117

130-
console_without_message "bundle exec rpsec"
118+
console_without_message "bundle exec rspec"
131119

132120
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!"
133121
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
137125
message <<-MARKDOWN
138126
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.
139127

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.
141129

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:
143131

144132
MARKDOWN
145133

146134
img src: "img/rails-test-types.png", alt: "Thoughtbot's diagram of types of Rails tests"
147135

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/
142+
143+
MARKDOWN
144+
148145
next_step "additional_concepts"

sites/en/testing/what_are_tests.step

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Remember that we need to model an orange and a tree. So, we know that there are
1010

1111
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.
1212

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.
1414

1515
# Why Is It Important That We Test?
1616
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

Comments
 (0)