Skip to content

Commit 7689273

Browse files
schaui6tjgrathwell
authored andcommitted
Add what are tests and testing frameworks
1 parent 7a94aa9 commit 7689273

File tree

2 files changed

+110
-4
lines changed

2 files changed

+110
-4
lines changed
Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,97 @@
11
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+
394

4-
Bla bla bla.
595
MARKDOWN
696

797
next_step "types_of_tests"

sites/en/testing/what_are_tests.step

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
message <<-MARKDOWN
2-
What are tests?
2+
# What Are Tests?
3+
Tests are ways of testing our code to see if they are performing the way we have intended it behave.
4+
5+
# Example
6+
For example, lets say we wanted to create a program that models an orange tree. Well, what defines an orange tree to us? Is it safe to say that we need at least two objects, an orange object and a tree object? Can our tree mature and bear more fruit at a certain age? Could our oranges ripen and fall from the tree? Sounds like the list can become lengthy right? Lets stop here for a few minutes and see what we need to fulfill these user stories.
7+
8+
# What We Need To Test?
9+
Remember that we need to model an orange and a tree, so we know that there are two objects that needs to be created.
10+
11+
We could say that the tree will not bear fruit until it matures at one year of age and then it well bare an X amount of oranges. We would need a test to test if the tree ages and a test to see if the tree has created an X amount of oranges once it has matured.
12+
13+
How do we determine if an orange is ripe? Well, we can have oranges 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.
14+
15+
# Now That We Have Tests
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?
17+
18+
# Why Is It Important That We Test?
19+
For situations like our tree's lifespan is a prime example of why tests are important. As our program become more complex over time the tests tells us that a basic requirement is not fulfilled and needs to be addressed. Can you imagine looking at pages and pages of code that was written by someone else or many years ago without a way to trace exactly where the bug is? Tests can help!
320

4-
Bla bla bla.
521
MARKDOWN
622

723
next_step "testing_frameworks"

0 commit comments

Comments
 (0)