Skip to content

Commit d098a8f

Browse files
author
Chang-Woo Rhee
committed
Added rspec
1 parent 4e8e23b commit d098a8f

File tree

7 files changed

+333
-1
lines changed

7 files changed

+333
-1
lines changed

.rspec

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

app/controllers/posts_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class PostsController < ApplicationController
2-
before_action :authorize_request
2+
before_action :authorize_request
33
before_action :set_post, only: [:show, :update, :destroy]
44
before_action only: [:edit, :update, :destroy] do
55
is_owner_object @post ##your object
+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
require 'rails_helper'
2+
3+
# This spec was generated by rspec-rails when you ran the scaffold generator.
4+
# It demonstrates how one might use RSpec to specify the controller code that
5+
# was generated by Rails when you ran the scaffold generator.
6+
#
7+
# It assumes that the implementation code is generated by the rails scaffold
8+
# generator. If you are using any extension libraries to generate different
9+
# controller code, this generated spec may or may not pass.
10+
#
11+
# It only uses APIs available in rails and/or rspec-rails. There are a number
12+
# of tools you can use to make these specs even more expressive, but we're
13+
# sticking to rails and rspec-rails APIs to keep things simple and stable.
14+
#
15+
# Compared to earlier versions of this generator, there is very limited use of
16+
# stubs and message expectations in this spec. Stubs are only used when there
17+
# is no simpler way to get a handle on the object needed for the example.
18+
# Message expectations are only used when there is no simpler way to specify
19+
# that an instance is receiving a specific message.
20+
#
21+
# Also compared to earlier versions of this generator, there are no longer any
22+
# expectations of assigns and templates rendered. These features have been
23+
# removed from Rails core in Rails 5, but can be added back in via the
24+
# `rails-controller-testing` gem.
25+
26+
RSpec.describe PostsController, type: :controller do
27+
28+
# This should return the minimal set of attributes required to create a valid
29+
# Post. As you add validations to Post, be sure to
30+
# adjust the attributes here as well.
31+
let(:user){
32+
User.save(name: "hello", username:"aaaa", email:"[email protected]", password: "ahahqq1234")
33+
}
34+
let(:valid_attributes) {
35+
Post.new()
36+
}
37+
38+
let(:invalid_attributes) {
39+
skip("Add a hash of attributes invalid for your model")
40+
}
41+
42+
# This should return the minimal set of values that should be in the session
43+
# in order to pass any filters (e.g. authentication) defined in
44+
# PostsController. Be sure to keep this updated too.
45+
let(:valid_session) { {} }
46+
47+
describe "GET #index" do
48+
it "returns a success response" do
49+
post = Post.create! valid_attributes
50+
get :index, params: {}, session: valid_session
51+
expect(response).to be_successful
52+
end
53+
end
54+
55+
describe "GET #show" do
56+
it "returns a success response" do
57+
post = Post.create! valid_attributes
58+
get :show, params: {id: post.to_param}, session: valid_session
59+
expect(response).to be_successful
60+
end
61+
end
62+
63+
describe "POST #create" do
64+
context "with valid params" do
65+
it "creates a new Post" do
66+
expect {
67+
post :create, params: {post: valid_attributes}, session: valid_session
68+
}.to change(Post, :count).by(1)
69+
end
70+
71+
it "renders a JSON response with the new post" do
72+
73+
post :create, params: {post: valid_attributes}, session: valid_session
74+
expect(response).to have_http_status(:created)
75+
expect(response.content_type).to eq('application/json')
76+
expect(response.location).to eq(post_url(Post.last))
77+
end
78+
end
79+
80+
context "with invalid params" do
81+
it "renders a JSON response with errors for the new post" do
82+
83+
post :create, params: {post: invalid_attributes}, session: valid_session
84+
expect(response).to have_http_status(:unprocessable_entity)
85+
expect(response.content_type).to eq('application/json')
86+
end
87+
end
88+
end
89+
90+
describe "PUT #update" do
91+
context "with valid params" do
92+
let(:new_attributes) {
93+
skip("Add a hash of attributes valid for your model")
94+
}
95+
96+
it "updates the requested post" do
97+
post = Post.create! valid_attributes
98+
put :update, params: {id: post.to_param, post: new_attributes}, session: valid_session
99+
post.reload
100+
skip("Add assertions for updated state")
101+
end
102+
103+
it "renders a JSON response with the post" do
104+
post = Post.create! valid_attributes
105+
106+
put :update, params: {id: post.to_param, post: valid_attributes}, session: valid_session
107+
expect(response).to have_http_status(:ok)
108+
expect(response.content_type).to eq('application/json')
109+
end
110+
end
111+
112+
context "with invalid params" do
113+
it "renders a JSON response with errors for the post" do
114+
post = Post.create! valid_attributes
115+
116+
put :update, params: {id: post.to_param, post: invalid_attributes}, session: valid_session
117+
expect(response).to have_http_status(:unprocessable_entity)
118+
expect(response.content_type).to eq('application/json')
119+
end
120+
end
121+
end
122+
123+
describe "DELETE #destroy" do
124+
it "destroys the requested post" do
125+
post = Post.create! valid_attributes
126+
expect {
127+
delete :destroy, params: {id: post.to_param}, session: valid_session
128+
}.to change(Post, :count).by(-1)
129+
end
130+
end
131+
132+
end

spec/rails_helper.rb

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# This file is copied to spec/ when you run 'rails generate rspec:install'
2+
require 'spec_helper'
3+
ENV['RAILS_ENV'] ||= 'test'
4+
5+
require File.expand_path('../config/environment', __dir__)
6+
7+
# Prevent database truncation if the environment is production
8+
abort("The Rails environment is running in production mode!") if Rails.env.production?
9+
require 'rspec/rails'
10+
# Add additional requires below this line. Rails is not loaded until this point!
11+
12+
# Requires supporting ruby files with custom matchers and macros, etc, in
13+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
14+
# run as spec files by default. This means that files in spec/support that end
15+
# in _spec.rb will both be required and run as specs, causing the specs to be
16+
# run twice. It is recommended that you do not name files matching this glob to
17+
# end with _spec.rb. You can configure this pattern with the --pattern
18+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
19+
#
20+
# The following line is provided for convenience purposes. It has the downside
21+
# of increasing the boot-up time by auto-requiring all files in the support
22+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
23+
# require only the support files necessary.
24+
#
25+
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
26+
27+
# Checks for pending migrations and applies them before tests are run.
28+
# If you are not using ActiveRecord, you can remove these lines.
29+
begin
30+
ActiveRecord::Migration.maintain_test_schema!
31+
rescue ActiveRecord::PendingMigrationError => e
32+
puts e.to_s.strip
33+
exit 1
34+
end
35+
RSpec.configure do |config|
36+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
37+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
38+
39+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
40+
# examples within a transaction, remove the following line or assign false
41+
# instead of true.
42+
config.use_transactional_fixtures = true
43+
44+
# RSpec Rails can automatically mix in different behaviours to your tests
45+
# based on their file location, for example enabling you to call `get` and
46+
# `post` in specs under `spec/controllers`.
47+
#
48+
# You can disable this behaviour by removing the line below, and instead
49+
# explicitly tag your specs with their type, e.g.:
50+
#
51+
# RSpec.describe UsersController, :type => :controller do
52+
# # ...
53+
# end
54+
#
55+
# The different available types are documented in the features, such as in
56+
# https://relishapp.com/rspec/rspec-rails/docs
57+
config.infer_spec_type_from_file_location!
58+
59+
# Filter lines from Rails gems in backtraces.
60+
config.filter_rails_from_backtrace!
61+
# arbitrary gems may also be filtered via:
62+
# config.filter_gems_from_backtrace("gem name")
63+
end

spec/requests/posts_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "Posts", type: :request do
4+
describe "GET /posts" do
5+
it "works! (now write some real specs)" do
6+
get posts_path
7+
expect(response).to have_http_status(200)
8+
end
9+
end
10+
end

spec/routing/posts_routing_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require "rails_helper"
2+
3+
RSpec.describe PostsController, type: :routing do
4+
describe "routing" do
5+
it "routes to #index" do
6+
expect(:get => "/posts").to route_to("posts#index")
7+
end
8+
9+
it "routes to #show" do
10+
expect(:get => "/posts/1").to route_to("posts#show", :id => "1")
11+
end
12+
13+
14+
it "routes to #create" do
15+
expect(:post => "/posts").to route_to("posts#create")
16+
end
17+
18+
it "routes to #update via PUT" do
19+
expect(:put => "/posts/1").to route_to("posts#update", :id => "1")
20+
end
21+
22+
it "routes to #update via PATCH" do
23+
expect(:patch => "/posts/1").to route_to("posts#update", :id => "1")
24+
end
25+
26+
it "routes to #destroy" do
27+
expect(:delete => "/posts/1").to route_to("posts#destroy", :id => "1")
28+
end
29+
end
30+
end

spec/spec_helper.rb

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# The generated `.rspec` file contains `--require spec_helper` which will cause
4+
# this file to always be loaded, without a need to explicitly require it in any
5+
# files.
6+
#
7+
# Given that it is always loaded, you are encouraged to keep this file as
8+
# light-weight as possible. Requiring heavyweight dependencies from this file
9+
# will add to the boot time of your test suite on EVERY test run, even for an
10+
# individual file that may not need all of that loaded. Instead, consider making
11+
# a separate helper file that requires the additional dependencies and performs
12+
# the additional setup, and require it from the spec files that actually need
13+
# it.
14+
#
15+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16+
RSpec.configure do |config|
17+
# rspec-expectations config goes here. You can use an alternate
18+
# assertion/expectation library such as wrong or the stdlib/minitest
19+
# assertions if you prefer.
20+
config.expect_with :rspec do |expectations|
21+
# This option will default to `true` in RSpec 4. It makes the `description`
22+
# and `failure_message` of custom matchers include text for helper methods
23+
# defined using `chain`, e.g.:
24+
# be_bigger_than(2).and_smaller_than(4).description
25+
# # => "be bigger than 2 and smaller than 4"
26+
# ...rather than:
27+
# # => "be bigger than 2"
28+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29+
end
30+
31+
# rspec-mocks config goes here. You can use an alternate test double
32+
# library (such as bogus or mocha) by changing the `mock_with` option here.
33+
config.mock_with :rspec do |mocks|
34+
# Prevents you from mocking or stubbing a method that does not exist on
35+
# a real object. This is generally recommended, and will default to
36+
# `true` in RSpec 4.
37+
mocks.verify_partial_doubles = true
38+
end
39+
40+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41+
# have no way to turn it off -- the option exists only for backwards
42+
# compatibility in RSpec 3). It causes shared context metadata to be
43+
# inherited by the metadata hash of host groups and examples, rather than
44+
# triggering implicit auto-inclusion in groups with matching metadata.
45+
config.shared_context_metadata_behavior = :apply_to_host_groups
46+
47+
# The settings below are suggested to provide a good initial experience
48+
# with RSpec, but feel free to customize to your heart's content.
49+
=begin
50+
# This allows you to limit a spec run to individual examples or groups
51+
# you care about by tagging them with `:focus` metadata. When nothing
52+
# is tagged with `:focus`, all examples get run. RSpec also provides
53+
# aliases for `it`, `describe`, and `context` that include `:focus`
54+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55+
config.filter_run_when_matching :focus
56+
57+
# Allows RSpec to persist some state between runs in order to support
58+
# the `--only-failures` and `--next-failure` CLI options. We recommend
59+
# you configure your source control system to ignore this file.
60+
config.example_status_persistence_file_path = "spec/examples.txt"
61+
62+
# Limits the available syntax to the non-monkey patched syntax that is
63+
# recommended. For more details, see:
64+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
65+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
66+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
67+
config.disable_monkey_patching!
68+
69+
# Many RSpec users commonly either run the entire suite or an individual
70+
# file, and it's useful to allow more verbose output when running an
71+
# individual spec file.
72+
if config.files_to_run.one?
73+
# Use the documentation formatter for detailed output,
74+
# unless a formatter has already been configured
75+
# (e.g. via a command-line flag).
76+
config.default_formatter = "doc"
77+
end
78+
79+
# Print the 10 slowest examples and example groups at the
80+
# end of the spec run, to help surface which specs are running
81+
# particularly slow.
82+
config.profile_examples = 10
83+
84+
# Run specs in random order to surface order dependencies. If you find an
85+
# order dependency and want to debug it, you can fix the order by providing
86+
# the seed, which is printed after each run.
87+
# --seed 1234
88+
config.order = :random
89+
90+
# Seed global randomization in this process using the `--seed` CLI option.
91+
# Setting this allows you to use `--seed` to deterministically reproduce
92+
# test failures related to randomization by passing the same `--seed` value
93+
# as the one that triggered the failure.
94+
Kernel.srand config.seed
95+
=end
96+
end

0 commit comments

Comments
 (0)