Skip to content

Commit 06d05ab

Browse files
authored
Merge pull request #2727 from threeplanetssoftware/update-documentation-for-code-blocks
Improve code blocks in documentation
2 parents bce9bd1 + 02fe0f7 commit 06d05ab

File tree

11 files changed

+176
-115
lines changed

11 files changed

+176
-115
lines changed

features/Generators.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ out of the box for with Rails' defaults.
1010

1111
RSpec generators can also be run independently. For instance,
1212

13-
rails generate rspec:model widget
13+
```console
14+
rails generate rspec:model widget
15+
```
1416

1517
will create a new spec file in `spec/models/widget_spec.rb`.
1618

features/GettingStarted.md

+28-10
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,62 @@
22

33
Install Rails 6
44

5-
$ gem install rails -v "~> 6.0.0"
5+
```console
6+
$ gem install rails -v "~> 6.0.0"
7+
```
68

79
### Generate an app
810

9-
$ rails new example_app
10-
$ cd example_app
11+
```console
12+
$ rails new example_app
13+
$ cd example_app
14+
```
1115

1216
### Add `rspec-rails` to the Gemfile
1317

14-
$ echo 'gem "rspec-rails", group: [:development, :test]' >> Gemfile
18+
```console
19+
$ echo 'gem "rspec-rails", group: [:development, :test]' >> Gemfile
20+
```
1521

1622
### Install the bundle
1723

18-
$ bundle install
24+
```console
25+
$ bundle install
26+
```
1927

2028
### Bootstrap RSpec
2129

22-
$ rails generate rspec:install
30+
```console
31+
$ rails generate rspec:install
32+
```
2333

2434
### Generate a scaffold
2535

26-
$ rails generate scaffold Widget name:string
36+
```console
37+
$ rails generate scaffold Widget name:string
38+
```
2739

2840
This generates files in the `app` and `spec` directories. The files in the
2941
`app` directory are generated by Rails, and Rails delegates the generation of
3042
the files in the `spec` directory to RSpec.
3143

3244
### Run migrations
3345

34-
$ rails db:migrate && rails db:test:prepare
46+
```console
47+
$ rails db:migrate && rails db:test:prepare
48+
```
3549

3650
### Run RSpec
3751

38-
$ rake spec
52+
```console
53+
$ rake spec
54+
```
3955

4056
or
4157

42-
$ rspec spec --format documentation
58+
```console
59+
$ rspec spec --format documentation
60+
```
4361

4462
If all went well, you should see output ending with:
4563

features/README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ for even older versions.
1515

1616
## Install
1717

18-
gem install rspec-rails
18+
```console
19+
gem install rspec-rails
20+
```
1921

2022
This installs the following gems:
2123

@@ -29,23 +31,29 @@ This installs the following gems:
2931

3032
Add rspec-rails to the :test and :development groups in the Gemfile:
3133

32-
group :test, :development do
33-
gem 'rspec-rails', '~> 6.0.0'
34-
end
34+
```ruby
35+
group :test, :development do
36+
gem 'rspec-rails', '~> 6.0.0'
37+
end
38+
```
3539

3640
It needs to be in the :development group to expose generators and rake tasks
3741
without having to type RAILS_ENV=test.
3842

3943
Now you can run:
4044

41-
bundle exec rails generate rspec:install
45+
```console
46+
bundle exec rails generate rspec:install
47+
```
4248

4349
This adds the spec directory and some skeleton files, including a .rspec
4450
file.
4551

4652
You can also customize the default spec path with `--default-path` option:
4753

48-
bundle exec rails generate rspec:install --default-path behaviour
54+
```console
55+
bundle exec rails generate rspec:install --default-path behaviour
56+
```
4957

5058
## Issues
5159

features/Transactions.md

+43-33
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
When you run `rails generate rspec:install`, the `spec/rails_helper.rb` file
44
includes the following configuration:
55

6-
RSpec.configure do |config|
7-
config.use_transactional_fixtures = true
8-
end
6+
```ruby
7+
RSpec.configure do |config|
8+
config.use_transactional_fixtures = true
9+
end
10+
```
911

1012
The name of this setting is a bit misleading. What it really means in Rails
1113
is "run every test method within a transaction." In the context of rspec-rails,
@@ -21,9 +23,11 @@ If you prefer to manage the data yourself, or using another tool like
2123
[database_cleaner](https://github.com/bmabey/database_cleaner) to do it for you,
2224
simply tell RSpec to tell Rails not to manage transactions:
2325

24-
RSpec.configure do |config|
25-
config.use_transactional_fixtures = false
26-
end
26+
```ruby
27+
RSpec.configure do |config|
28+
config.use_transactional_fixtures = false
29+
end
30+
```
2731

2832
### Data created in `before(:example)` are rolled back
2933

@@ -32,19 +36,21 @@ the example. This is a good thing because it means that each example is
3236
isolated from state that would otherwise be left around by the examples that
3337
already ran. For example:
3438

35-
describe Widget do
36-
before(:example) do
37-
@widget = Widget.create
38-
end
39+
```ruby
40+
describe Widget do
41+
before(:example) do
42+
@widget = Widget.create
43+
end
3944

40-
it "does something" do
41-
expect(@widget).to do_something
42-
end
45+
it "does something" do
46+
expect(@widget).to do_something
47+
end
4348

44-
it "does something else" do
45-
expect(@widget).to do_something_else
46-
end
47-
end
49+
it "does something else" do
50+
expect(@widget).to do_something_else
51+
end
52+
end
53+
```
4854

4955
The `@widget` is recreated in each of the two examples above, so each example
5056
has a different object, _and_ the underlying data is rolled back so the data
@@ -60,27 +66,31 @@ guidelines:
6066

6167
1. Be sure to clean up any data in an `after(:context)` hook:
6268

63-
before(:context) do
64-
@widget = Widget.create!
65-
end
69+
```ruby
70+
before(:context) do
71+
@widget = Widget.create!
72+
end
6673

67-
after(:context) do
68-
@widget.destroy
69-
end
74+
after(:context) do
75+
@widget.destroy
76+
end
77+
```
7078

7179
If you don't do that, you'll leave data lying around that will eventually
72-
interfere with other examples.
80+
interfere with other examples.
7381

7482
2. Reload the object in a `before(:example)` hook.
7583

76-
before(:context) do
77-
@widget = Widget.create!
78-
end
84+
```ruby
85+
before(:context) do
86+
@widget = Widget.create!
87+
end
7988
80-
before(:example) do
81-
@widget.reload
82-
end
89+
before(:example) do
90+
@widget.reload
91+
end
92+
```
8393

84-
Even though database updates in each example will be rolled back, the
85-
object won't _know_ about those rollbacks so the object and its backing
86-
data can easily get out of sync.
94+
Even though database updates in each example will be rolled back, the
95+
object won't _know_ about those rollbacks so the object and its backing
96+
data can easily get out of sync.

features/controller_specs/README.md

+26-22
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,23 @@ To specify outcomes, you can use:
4242

4343
## Examples
4444

45-
RSpec.describe TeamsController do
46-
describe "GET index" do
47-
it "assigns @teams" do
48-
team = Team.create
49-
get :index
50-
expect(assigns(:teams)).to eq([team])
51-
end
52-
53-
it "renders the index template" do
54-
get :index
55-
expect(response).to render_template("index")
56-
end
57-
end
45+
```ruby
46+
RSpec.describe TeamsController do
47+
describe "GET index" do
48+
it "assigns @teams" do
49+
team = Team.create
50+
get :index
51+
expect(assigns(:teams)).to eq([team])
5852
end
5953
54+
it "renders the index template" do
55+
get :index
56+
expect(response).to render_template("index")
57+
end
58+
end
59+
end
60+
```
61+
6062
## Views
6163

6264
* by default, views are not rendered. See
@@ -67,14 +69,16 @@ To specify outcomes, you can use:
6769

6870
We encourage you to use [request specs](./request-specs/request-spec) if you want to set headers in your call. If you still want to use controller specs with custom http headers you can use `request.headers`:
6971

70-
require "rails_helper"
72+
```ruby
73+
require "rails_helper"
7174
72-
RSpec.describe TeamsController, type: :controller do
73-
describe "GET index" do
74-
it "returns a 200" do
75-
request.headers["Authorization"] = "foo"
76-
get :show
77-
expect(response).to have_http_status(:ok)
78-
end
79-
end
75+
RSpec.describe TeamsController, type: :controller do
76+
describe "GET index" do
77+
it "returns a 200" do
78+
request.headers["Authorization"] = "foo"
79+
get :show
80+
expect(response).to have_http_status(:ok)
8081
end
82+
end
83+
end
84+
```

features/mailer_specs/README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ behavior and expectations.
99

1010
## Examples
1111

12-
require "rails_helper"
12+
```ruby
13+
require "rails_helper"
1314

14-
RSpec.describe Notifications, type: :mailer do
15-
describe "notify" do
16-
let(:mail) { Notifications.signup }
15+
RSpec.describe Notifications, type: :mailer do
16+
describe "notify" do
17+
let(:mail) { Notifications.signup }
1718

18-
it "renders the headers" do
19-
expect(mail.subject).to eq("Signup")
20-
expect(mail.to).to eq(["[email protected]"])
21-
expect(mail.from).to eq(["[email protected]"])
22-
end
19+
it "renders the headers" do
20+
expect(mail.subject).to eq("Signup")
21+
expect(mail.to).to eq(["[email protected]"])
22+
expect(mail.from).to eq(["[email protected]"])
23+
end
2324

24-
it "renders the body" do
25-
expect(mail.body.encoded).to match("Hi")
26-
end
27-
end
25+
it "renders the body" do
26+
expect(mail.body.encoded).to match("Hi")
2827
end
28+
end
29+
end
30+
```

features/matchers/README.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ rspec-compatible wrappers for Rails' assertions.
55

66
### redirects
77

8-
# delegates to assert_redirected_to
9-
expect(response).to redirect_to(path)
8+
```ruby
9+
# delegates to assert_redirected_to
10+
expect(response).to redirect_to(path)
11+
```
1012

1113
### templates
1214

13-
# delegates to assert_template
14-
expect(response).to render_template(template_name)
15+
```ruby
16+
# delegates to assert_template
17+
expect(response).to render_template(template_name)
18+
```
1519

1620
### assigned objects
1721

18-
# passes if assigns(:widget) is an instance of Widget
19-
# and it is not persisted
20-
expect(assigns(:widget)).to be_a_new(Widget)
22+
```ruby
23+
# passes if assigns(:widget) is an instance of Widget
24+
# and it is not persisted
25+
expect(assigns(:widget)).to be_a_new(Widget)
26+
```

features/model_specs/README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ behavior and expectations.
99

1010
## Examples
1111

12-
require "rails_helper"
12+
```ruby
13+
require "rails_helper"
1314

14-
RSpec.describe Post, type: :model do
15-
context "with 2 or more comments" do
16-
it "orders them in reverse chronologically" do
17-
post = Post.create!
18-
comment1 = post.comments.create!(:body => "first comment")
19-
comment2 = post.comments.create!(:body => "second comment")
20-
expect(post.reload.comments).to eq([comment2, comment1])
21-
end
22-
end
15+
RSpec.describe Post, type: :model do
16+
context "with 2 or more comments" do
17+
it "orders them in reverse chronologically" do
18+
post = Post.create!
19+
comment1 = post.comments.create!(:body => "first comment")
20+
comment2 = post.comments.create!(:body => "second comment")
21+
expect(post.reload.comments).to eq([comment2, comment1])
2322
end
23+
end
24+
end
25+
```

0 commit comments

Comments
 (0)