Skip to content

Commit 7a94aa9

Browse files
akanshmurthytjgrathwell
authored andcommitted
add additional concepts section
1 parent 2b06ae1 commit 7a94aa9

File tree

5 files changed

+84
-12
lines changed

5 files changed

+84
-12
lines changed

sites/en/docs/docs.step

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Build a message board! This curriculum is for students who have completed the Su
2525
MARKDOWN
2626

2727
site_desc 'testing', <<-MARKDOWN
28-
Increase the stability of your Rails app by learning about tests: what they are, why they're used, and how to use them! This curriculum is for students who have completed the Suggestotron and the Job Board curricula. This curriculum is open to everyone but there is a challenge at the end!
28+
Increase the stability of your Rails app by learning about tests: what they are, why they're used, and how to use them! This curriculum is for students who have completed the Suggestotron, the Job Board, and the Message Board curricula. There will be challenges!
2929
MARKDOWN
3030

3131
h1 'Frontend'
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
message <<-MARKDOWN
2+
All right, you're almost at the end! Below are a few additional concepts of testing that may helpful in your future testing adventures.
3+
4+
### Doubles and stubs
5+
Doubles are simpler objects that represent objects from your application.
6+
<div class="console"><pre>
7+
post = double(:post)
8+
</pre>
9+
</div>
10+
If you instantiate that double in your test file, you have access to post in your tests to test with. This is instead of creating an entire Post model in ActiveRecord. If you need to create many different test objects with different properties, FactoryGirl is a great gem for that purpose and will allow persistence or in-memory object creation, depending on your testing situation.
11+
12+
Stubs can be used to dictate what is returned when a method is called on a double.
13+
<div class="console"><pre>
14+
post.stub(:title).and_return("Jelly")
15+
</pre>
16+
</div>
17+
So, when you write a test that calls the title attribute of the post double, you'll always get back the string Jelly. Got it? Good!
18+
19+
### Spies
20+
With spies, we are not talking about espionage... at least, not in relation to testing :) Spies can be used to verify whether a method was called on an object.
21+
For instance (assume you already have the post double from above):
22+
<div class="console"><pre>
23+
post = spy('post')
24+
post.content
25+
expect(post).to have_received(:content)
26+
</pre>
27+
</div>
28+
Obviously, this is a simplified case. Instead of post.content, you might have a complicated method that executes many functions internally and that's where spies can come in handy; they can check easily whether one specific method was called. Capiche? Ok, let's keep on trucking!
29+
30+
### Webmock
31+
What if your app relies on third-party services or applications, known amongst friends as application programming interfaces or APIs? Well, it seems like APIs should also be tested but should our test suite really be dependent on someone else? NOPE! What if the API goes down? Or is slow? Welcome to the stage: Webmock!
32+
Webmock is a gem that stubs out external HTTP requests. Once you include the gem, bundle install, and include the below code snippet in your spec helper file (which is included in every test file), you're good to go.
33+
MARKDOWN
34+
console_without_message <<-RUBY
35+
require 'webmock/rspec'
36+
WebMock.disable_net_connect!(allow_localhost: true)
37+
RUBY
38+
39+
message <<-MARKDOWN
40+
Then, you can start stubbing out API requests in your spec helper file. Let's write an example for Bitly, a service that shortens long URLs.
41+
MARKDOWN
42+
43+
console_without_message <<-RUBY
44+
RSpec.configure do |config|
45+
config.before(:each) do
46+
stub_request(:get, /api.bitly.com.v3.shorten/).
47+
with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
48+
to_return(status: 200, body: "stubbed response", headers: {})
49+
end
50+
end
51+
RUBY
52+
53+
message <<-MARKDOWN
54+
So, if you write any tests in your test files that call the Bitly API, then the response will be whatever you defined above. The test will prevent the actual API request from being made. Pretty cool, huh?
55+
56+
Awesome, you are now equipped with a license to TEST! Go forth and create doubles, stubs, and spies in your app (at least one of each and have a TA verify).
57+
MARKDOWN

sites/en/testing/advanced_concepts.step

-5
This file was deleted.

sites/en/testing/testing.step

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
message <<-MARKDOWN
2-
Bla bla bla.
2+
### Goal
3+
4+
To teach you testing we are going to start with the basics and have you learn by doing through small challenges and then a final large.
5+
6+
When you have completed this curriculum you should understand:
7+
8+
* what tests are
9+
* why they're used
10+
* how they're used
11+
* what types of tests exist
12+
* what types of frameworks exist
13+
* some additional concepts in testing such as doubles, stubs, spies, and Webmock
14+
* how to write tests
15+
* how to break tests
16+
* how to fix tests
17+
18+
### Requirements
19+
20+
We're going to be working with:
21+
22+
* Ruby on Rails
23+
* A command line program
24+
* A text editor of your choice
325

4-
Bla bla bla.
526
MARKDOWN
627

728
tip "This is not a self-paced curriculum. You should use the discussion sections on each page to make sure everyone is together!"
829

930
message <<-MARKDOWN
10-
# Notable Things
31+
### Notable things
1132

1233
As you might have noticed, we're assuming you've already been to a RailsBridge workshop before or have otherwise already explored a Rails app, and are ready for deeper knowledge.
1334

14-
We're also going to skip deploying to Heroku this time around, but you can definitely use the instructions from the Suggestotron curriculum to deploy your app to the internet!
1535
MARKDOWN
1636

1737
important "This curriculum is written for Rails 5. Things will get awkward / broken if you're using an earlier version of Rails, so if you skipped the Installfest, you need to upgrade to Rails 5 now."
1838

1939
message <<-MARKDOWN
20-
# Tips for everyone:
40+
### Tips for everyone
2141

2242
* When adding code, it's awesome for students to walk through the code line by line and say out loud what is happening. (i.e., "The string is being stored in the instance variable" or "The method `snorgle` is being defined"). If you do it every time, you'll get really comfortable with the vocabulary of Rails!
2343
* Error messages are your friend! Read them carefully, and practice understanding what Rails is telling you. Seeing an error and just diving back into your code is a natural reaction, but stop! Then read, think, and talk about what the error means before fixing it.

sites/en/testing/types_of_tests.step

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ MARKDOWN
143143

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

146-
next_step "advanced_concepts"
146+
next_step "additional_concepts"

0 commit comments

Comments
 (0)