FactoryBotでのテストデータ作成について
RailsのAPIモードでのmodelテストデータ作成の為、Rspec、FactoryBotを使用したいと考えております。
FactoryBot.create()メソッドでテストDBにテストデータの挿入を行いたいのですが、データの挿入がされない状態です。
ruby
1#factories/user.rb 2 3FactoryBot.define do 4 factory :user do 5 name 'test' 6 end 7end
ruby
1#models/user_spec.rb 2require 'rails_helper' 3 4RSpec.describe User, type: :model do 5 describe 'test' do 6 it 'test' do 7 FactoryBot.create(:user) 8 end 9 end 10end
Gemの構成は下記のとおりです。
gem
1source 'https://rubygems.org' 2 3git_source(:github) do |repo_name| 4 repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") 5 "https://github.com/#{repo_name}.git" 6end 7 8 9# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 10gem 'rails', '~> 5.1.6' 11# Use mysql as the database for Active Record 12gem 'mysql2', '>= 0.3.18', '< 0.6.0' 13# Use Puma as the app server 14gem 'puma', '~> 3.7' 15# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 16# gem 'jbuilder', '~> 2.5' 17# Use Redis adapter to run Action Cable in production 18# gem 'redis', '~> 4.0' 19# Use ActiveModel has_secure_password 20# gem 'bcrypt', '~> 3.1.7' 21 22# Use Capistrano for deployment 23# gem 'capistrano-rails', group: :development 24 25# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible 26# gem 'rack-cors' 27 28group :development, :test do 29 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 30 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 31end 32 33group :development do 34 gem 'listen', '>= 3.0.5', '< 3.2' 35 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 36 gem 'spring' 37 gem 'spring-watcher-listen', '~> 2.0.0' 38 gem 'rspec' 39 gem 'rspec-rails' 40 gem 'factory_bot_rails' 41 42 gem 'faker', '~> 1.1.2' 43 gem 'capybara', '~> 2.2.0' 44 gem 'launchy', '~> 2.3.0' 45 gem 'selenium-webdriver', '~>2.45.0' 46 gem 'guard-rspec', require: false 47 48end 49 50# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 51gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Rspecのテスト自体は全て正常に終了しておりますが、
間違いなどございますでしょうか。もしくはFactoryBotはそれらしいデータを作るだけで、
DBへの反映はできないのでしょうか。
どうぞよろしくお願いいたします。
テストDBは下記のとおりです。(mysql環境です。)
|id:int|name:string|create_at:datetime|update_at:datetime|
|:--|:--:|--:|
回答2件
あなたの回答
tips
プレビュー