Skip to content

Commit

Permalink
respecとtest_codeの追加
Browse files Browse the repository at this point in the history
  • Loading branch information
soneda-yuya committed Sep 25, 2020
1 parent b833c4a commit 488f087
Show file tree
Hide file tree
Showing 16 changed files with 404 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--require spec_helper
9 changes: 9 additions & 0 deletions .rubocop.yml
Expand Up @@ -2,5 +2,14 @@ Layout/LineLength:
Exclude:
- 'db/**/*'

Metrics/BlockLength:
Exclude:
- 'db/schema.rb'
- 'db/migrate/*'

Style/GuardClause:
Exclude:
- 'db/migrate/*'

Documentation:
Enabled: false
8 changes: 8 additions & 0 deletions Gemfile
Expand Up @@ -34,6 +34,14 @@ gem 'bootsnap', '>= 1.4.2', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: %i[mri mingw x64_mingw]
gem 'factory_bot_rails'
gem 'rspec-rails'
end

group :test do
gem 'database_cleaner'
gem 'rails-controller-testing'
gem 'shoulda-matchers'
end

group :development do
Expand Down
35 changes: 35 additions & 0 deletions Gemfile.lock
Expand Up @@ -64,7 +64,14 @@ GEM
byebug (11.1.3)
concurrent-ruby (1.1.6)
crass (1.0.6)
database_cleaner (1.8.5)
diff-lcs (1.4.4)
erubi (1.9.0)
factory_bot (6.1.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
railties (>= 5.0.0)
faker (2.14.0)
i18n (>= 1.6, < 2)
ffi (1.13.1)
Expand Down Expand Up @@ -120,6 +127,10 @@ GEM
bundler (>= 1.3.0)
railties (= 6.0.2.1)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand All @@ -138,6 +149,23 @@ GEM
ffi (~> 1.0)
regexp_parser (1.8.0)
rexml (3.2.4)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-rails (4.0.1)
actionpack (>= 4.2)
activesupport (>= 4.2)
railties (>= 4.2)
rspec-core (~> 3.9)
rspec-expectations (~> 3.9)
rspec-mocks (~> 3.9)
rspec-support (~> 3.9)
rspec-support (3.9.3)
rubocop (0.91.1)
parallel (~> 1.10)
parser (>= 2.7.1.1)
Expand Down Expand Up @@ -170,6 +198,8 @@ GEM
sprockets (> 3.0)
sprockets-rails
tilt
shoulda-matchers (4.4.1)
activesupport (>= 4.2.0)
slim (4.1.0)
temple (>= 0.7.6, < 0.9)
tilt (>= 2.0.6, < 2.1)
Expand Down Expand Up @@ -211,17 +241,22 @@ PLATFORMS
DEPENDENCIES
bootsnap (>= 1.4.2)
byebug
database_cleaner
factory_bot_rails
faker
jbuilder (~> 2.7)
listen (>= 3.0.5, < 3.2)
mysql2 (>= 0.4.4)
puma (~> 4.1)
rails (~> 6.0.2, >= 6.0.2.1)
rails-controller-testing
rspec-rails
rubocop
rubocop-performance
rubocop-rails
rubocop-rspec
sass-rails (>= 6)
shoulda-matchers
slim
spring
spring-watcher-listen (~> 2.0.0)
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/categories.rb
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :category do
name { Faker::Movies::StarWars.character }
end
end
10 changes: 10 additions & 0 deletions spec/factories/category_setting_items.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true

FactoryBot.define do
factory :category_setting_item do
name { Faker::Movies::StarWars.character }
after(:build) do |r|
r.category ||= create(:category)
end
end
end
14 changes: 14 additions & 0 deletions spec/factories/product_category_settings.rb
@@ -0,0 +1,14 @@
# frozen_string_literal: true

FactoryBot.define do
factory :product_category_setting do
enabled { 1 }
after(:build) do |r|
r.product ||= create(:product)
r.category_setting_item ||= create(
:category_setting_item,
category: r.product.category
)
end
end
end
20 changes: 20 additions & 0 deletions spec/factories/products.rb
@@ -0,0 +1,20 @@
# frozen_string_literal: true

FactoryBot.define do
factory :product do
name { Faker::Movies::StarWars.character }
after(:build) do |r|
r.category ||= create(:category)
if r.product_category_settings.blank?
r.product_category_settings << create(
:product_category_setting,
product: r,
category_setting_item: create(
:category_setting_item,
category: r.category
)
)
end
end
end
end
15 changes: 15 additions & 0 deletions spec/models/category_setting_item_spec.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe CategorySettingItem, type: :model do
describe 'relations' do
it { is_expected.to belong_to :category }
it { is_expected.to have_many :product_category_settings }
end

describe 'create' do
subject { CategorySettingItem.create!(name: 'hoge', category: create(:category)) }
it { expect { subject }.to change { CategorySettingItem.all.count }.by(1) }
end
end
15 changes: 15 additions & 0 deletions spec/models/category_spec.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Category, type: :model do
describe 'relations' do
it { is_expected.to have_many :products }
it { is_expected.to have_many :category_setting_items }
end

describe 'create' do
subject { Category.create!(name: 'hoge') }
it { expect { subject }.to change { Category.all.count }.by(1) }
end
end
30 changes: 30 additions & 0 deletions spec/models/product_category_setting_spec.rb
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ProductCategorySetting, type: :model do
describe 'relations' do
it { is_expected.to belong_to :product }
it { is_expected.to belong_to :category_setting_item }
end

describe 'create' do
subject do
ProductCategorySetting.create!(
product: product,
category_setting_item: category_setting_item,
enabled: 1
)
end
let(:product) { create(:product) }
let(:category_setting_item) { create(:category_setting_item, category: product.category) }
it {
expect { subject }.to change {
ProductCategorySetting.where(
product: product,
category_setting_item: category_setting_item
).count
}.by(1)
}
end
end
15 changes: 15 additions & 0 deletions spec/models/product_spec.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Product, type: :model do
describe 'relations' do
it { is_expected.to belong_to :category }
it { is_expected.to have_many :product_category_settings }
end

describe 'create' do
subject { Product.create!(name: 'hoge', category: create(:category)) }
it { expect { subject }.to change { Product.all.count }.by(1) }
end
end
95 changes: 95 additions & 0 deletions spec/rails_helper.rb
@@ -0,0 +1,95 @@
# frozen_string_literal: true

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

config.include FactoryBot::Syntax::Methods

config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end

config.before(:all) do
DatabaseCleaner.start
end

config.after(:all) do
DatabaseCleaner.clean
end
end

Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
17 changes: 17 additions & 0 deletions spec/requests/categories_request_spec.rb
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Categories', type: :request do
describe 'GET /index' do
before { create(:product) }
it 'renders a successful response' do
get categories_path
expect(response).to be_successful
end
it 'render index template' do
get products_path
should render_template('index')
end
end
end
17 changes: 17 additions & 0 deletions spec/requests/product_request_spec.rb
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Products', type: :request do
describe 'GET /index' do
before { create(:product) }
it 'renders a successful response' do
get products_path
expect(response).to be_successful
end
it 'render index template' do
get products_path
should render_template('index')
end
end
end

0 comments on commit 488f087

Please sign in to comment.