Skip to content

Commit 64b09fc

Browse files
committed
install rspec
1 parent 5eb8930 commit 64b09fc

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
require "rails_helper"
2+
3+
RSpec.describe ProductsController, type: :controller do
4+
5+
describe "#index" do
6+
before do
7+
@all_products = Product.all
8+
get :index
9+
end
10+
11+
it "should assign @products" do
12+
expect(assigns(:products)).to eq(@all_products)
13+
end
14+
15+
it "should render the :index view" do
16+
expect(response).to render_template(:index)
17+
end
18+
end
19+
20+
describe "#new" do
21+
before do
22+
get :new
23+
end
24+
25+
it "should assign @product" do
26+
expect(assigns(:product)).to be_instance_of(Product)
27+
end
28+
29+
it "should render the :new view" do
30+
expect(response).to render_template(:new)
31+
end
32+
end
33+
34+
describe "#create" do
35+
context "success" do
36+
before do
37+
@products_count = Product.count
38+
post :create, product: { }
39+
end
40+
41+
it "should add new product to the database" do
42+
expect(Product.count).to eq(@products_count + 1)
43+
end
44+
45+
it "should redirect_to 'product_path'" do
46+
expect(response.status).to be(302)
47+
expect(response.location).to match(/\/products\/\d+/)
48+
end
49+
end
50+
51+
context "failed validations" do
52+
before do
53+
# create blank product (fails validations)
54+
post :create, product: { }
55+
end
56+
57+
it "should display an error message" do
58+
expect(flash[:error]).to be_present
59+
end
60+
61+
it "should redirect to 'new_product_path'" do
62+
expect(response.status).to be(302)
63+
expect(response).to redirect_to(new_product_path)
64+
end
65+
end
66+
end
67+
68+
describe "#show" do
69+
before do
70+
@product = FactoryGirl.create(:product)
71+
get :show, id: @product.id
72+
end
73+
74+
it "should assign @product" do
75+
expect(assigns(:product)).to eq(@product)
76+
end
77+
78+
it "should render the :show view" do
79+
expect(response).to render_template(:show)
80+
end
81+
end
82+
83+
describe "#edit" do
84+
before do
85+
@product = FactoryGirl.create(:product)
86+
get :edit, id: @product.id
87+
end
88+
89+
it "should assign @product" do
90+
expect(assigns(:product)).to eq(@product)
91+
end
92+
93+
it "should render the :edit view" do
94+
expect(response).to render_template(:edit)
95+
end
96+
end
97+
98+
describe "#update" do
99+
before do
100+
@product = FactoryGirl.create(:product)
101+
end
102+
103+
context "success" do
104+
before do
105+
@new_name =
106+
@new_description =
107+
@new_category =
108+
put :update, id: @product.id, product: { }
109+
110+
# reload @product to get changes from :update
111+
@product.reload
112+
end
113+
114+
it "should update product in the database" do
115+
expect(@product.name).to eq(@new_name)
116+
expect(@product.description).to eq(@new_description)
117+
end
118+
119+
it "should redirect_to 'product_path'" do
120+
expect(response.status).to be(302)
121+
expect(response).to redirect_to(product_path(@product))
122+
end
123+
end
124+
125+
context "failed validations" do
126+
before do
127+
# update with blank product params (fails validations)
128+
put :update, id: @product.id, product: { }
129+
end
130+
131+
it "should display an error message" do
132+
expect(flash[:error]).to be_present
133+
end
134+
135+
it "should redirect_to 'edit_product_path'" do
136+
expect(response).to redirect_to(edit_product_path)
137+
end
138+
end
139+
end
140+
141+
describe "#destroy" do
142+
before do
143+
product = FactoryGirl.create(:product)
144+
@all_products = Product.count
145+
delete :destroy, id: product.id
146+
end
147+
148+
it "should remove product from the database" do
149+
expect(Product.count).to eq(@all_products - 1)
150+
end
151+
152+
it "should redirect_to 'root_path'" do
153+
expect(response.status).to be(302)
154+
expect(response).to redirect_to(root_path)
155+
end
156+
end
157+
end

spec/rails_helper.rb

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

spec/spec_helper.rb

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

0 commit comments

Comments
 (0)