1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails ~Rspec環境~

Posted at

Rspec

Install

group :development, :test do
  gem 'rspec-rails', '~> 5.0.0'
end

rails generate rspec:install

Configuration

.rspec

.rspec
# 全てのファイルでrequireを宣言しなくてもspec_helperがロードされる
--require spec_helper
# 出力を色付け
--color
# 出力形式を詳細に記載
--format documantation

rails_helper.rbで require spec_helperがされているので、このファイルを .rspec内に記載すれば良いように思えるがspec_helper.rbファイルにコメントアウトされている記述によると、全てのファイルにロードされるものなのでできるだけ軽量な依存にとどめておくのがベターであると。

Factory bot rails

Install

group :development, :test do
  gem 'factory_bot_rails'
end

Configuration

spec/support/factory_bot.rb
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
rails_helper.rb
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

上記の設定で、FactoryBotのシンタックスがいらなくなる。
(https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rspec)

# before
FactoryBot.build(:user)

# after
build(:user)

Generators

上記の設定で railsをAPIモードで動かしている時、モデル、コントローラーを生成すると自動で

controller
spec/requests/users_spec.rb
model
spec/models/user_spec.rb

spec/factories/users.rb

が生成される。

Factory bot Generators: https://github.com/thoughtbot/factory_bot_rails#generators

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?