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
Copy file name to clipboardExpand all lines: sites/en/testing/types_of_tests.step
+17-16Lines changed: 17 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -3,23 +3,14 @@ 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
-
Below, you will create the Orange model in your app so that the model file looks something like this:
7
-
8
-
<div class="console"><pre>
9
-
class Orange < ActiveRecord::Base
10
-
belongs_to :tree
11
-
end
12
-
</pre>
13
-
</div>
14
-
15
6
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/
16
7
MARKDOWN
17
8
18
9
steps do
19
10
step do
20
11
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
12
22
-
console_without_message "rails g model Orange"
13
+
console_without_message "rails generate model Orange"
23
14
24
15
message "Then, run the migration to actually create the oranges table."
25
16
@@ -30,7 +21,8 @@ steps do
30
21
31
22
console_without_message "bundle exec rspec"
32
23
33
-
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."
24
+
message "You will see a report with one pending test. When you generated your Orange model, RSpec also generated a matching spec file. Copy the test below into spec/models/orange_spec.rb and run 'bundle exec rspec' on the terminal again."
25
+
34
26
35
27
console_without_message <<-RUBY
36
28
RSpec.describe Orange, :type => :model do
@@ -42,15 +34,24 @@ steps do
42
34
end
43
35
RUBY
44
36
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!"
37
+
message "Run 'bundle exec rspec'. This test fails! Let's add an associaton to our model. Add a belongs_to association to orange.rb:"
38
+
39
+
console_without_message <<-RUBY
40
+
class Orange < ActiveRecord::Base
41
+
belongs_to :tree
42
+
end
43
+
RUBY
46
44
45
+
message "Run 'bundle exec rspec' and 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."
47
46
end
48
-
end
49
47
48
+
step do
50
49
51
-
message <<-MARKDOWN
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.
53
-
MARKDOWN
50
+
message "Now let's write a has_many association test for the relationship between the Tree model and the Orange model! (hint: this doesn't exist yet so you'll have to create the model and migrate!)"
51
+
52
+
message "On to controller tests! Just like the Orange model, you will create the OrangesController, which will also create spec files in the /spec/controllers folder of your app."
0 commit comments