Skip to content
This repository was archived by the owner on Jul 21, 2024. It is now read-only.

03-models #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem 'jbuilder', '~> 2.5'

group :development, :test do
gem 'rspec-rails', '~> 3.6.0'
gem "factory_bot_rails", "~> 4.10.0"
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'capybara', '~> 2.13.0'
gem 'selenium-webdriver'
Expand Down
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ GEM
diff-lcs (1.3)
erubi (1.7.1)
execjs (2.7.0)
factory_bot (4.10.0)
activesupport (>= 3.0.0)
factory_bot_rails (4.10.0)
factory_bot (~> 4.10.0)
railties (>= 3.0.0)
faker (1.7.3)
i18n (~> 0.5)
ffi (1.9.18)
Expand Down Expand Up @@ -235,6 +240,7 @@ DEPENDENCIES
capybara (~> 2.13.0)
coffee-rails (~> 4.2)
devise
factory_bot_rails (~> 4.10.0)
faker
geocoder
jbuilder (~> 2.5)
Expand All @@ -256,4 +262,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
1.14.6
1.16.4
4 changes: 2 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class Application < Rails::Application

config.generators do |g|
g.test_framework :rspec,
fixtures: false,
view_specs: false,
helper_specs: false,
routing_specs: false
routing_specs: false,
request_specs: false
end
end
end
8 changes: 8 additions & 0 deletions spec/factories/notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :note do
message "My important note."
association :project
# association :user
user { project.owner }
end
end
72 changes: 72 additions & 0 deletions spec/factories/projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
FactoryBot.define do
factory :project do
sequence(:name) { |n| "Project #{n}" }
description "A test project."
due_on 1.week.from_now
association :owner

# メモ付きのプロジェクト
trait :with_notes do
after(:create) { |project| create_list(:note, 5, project: project) }
end

# 締め切りが昨日
trait :due_yesterday do
due_on 1.day.ago
end

# 締め切りが今日
trait :due_today do
due_on Date.current.in_time_zone
end

# 締め切りが明日
trait :due_tomorrow do
due_on 1.day.from_now
end

=begin
# 昨日が締め切りのプロジェクト
factory :project_due_yesterday do
due_on 1.day.ago
end

# 今日が締め切りのプロジェクト
factory :project_due_today do
due_on Date.current.in_time_zone
end

# 明日が締め切りのプロジェクト
factory :project_due_tomorrow do
due_on 1.day.from_now
end
=end

end

=begin
# 昨日が締め切りのプロジェクト
factory :project_due_yesterday, class: Project do
sequence(:name) { |n| "Test Project #{n}" }
description "Sample project for testing purposes"
due_on 1.day.ago
association :owner
end

# 今日が締め切りのプロジェクト
factory :project_due_today, class: Project do
sequence(:name) { |n| "Test Project #{n}" }
description "Sample project for testing purposes"
due_on Date.current.in_time_zone
association :owner
end

# 明日が締め切りのプロジェクト
factory :project_due_tomorrow, class: Project do
sequence(:name) { |n| "Test Project #{n}" }
description "Sample project for testing purposes"
due_on 1.day.from_now
association :owner
end
=end
end
19 changes: 19 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FactoryBot.define do
factory :user, aliases: [:owner] do
first_name "Aaron"
last_name "Sumner"
sequence(:email) { |n| "tester#{n}@example.com" }
password "dottle-nouveau-pavilion-tights-furze"
end
end

=begin
FactoryBot.define do
factory :user do
first_name "Aaron"
last_name "Sumner"
email "tester@example.com"
password "dottle-nouveau-pavilion-tights-furze"
end
end
=end
150 changes: 150 additions & 0 deletions spec/models/note_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
require 'rails_helper'

RSpec.describe Note, type: :model do

# ファクトリで関連するデータを生成する
it "generates associated data from a factory" do
note = FactoryBot.create(:note)
puts "This note's project is #{note.project.inspect}"
puts "This note's user is #{note.user.inspect}"
end


before do
# このファイルの全テストで使用するテストデータをセットアップする
@user = User.create(
first_name: "Joe",
last_name: "Tester",
email: "joetester@example.com",
password: "dottle-nouveau-pavilion-tights-furze",
)
@project = @user.projects.create(
name: "Test Project",
)
end

# バリデーションのテストが並ぶ

# ユーザ、プロジェクト、メッセージがあれば有効な状態であること
it "is valid with a user, project, and message" do
note = Note.new(
message: "This is a sample note.",
user: @user,
project: @project,
)

expect(note).to be_valid
end

# メッセージがなければ無効な状態であること
it "is invalid without a message" do
note = Note.new(message: nil)
note.valid?
expect(note.errors[:message]).to include("can't be blank")
end

# 文字列に一致するメッセージを検索する
describe "search message for a term" do

before do
# 検索機能の全テストに関連する追加のテストデータをセットアップする
@note1 = @project.notes.create(
message: "This is the first note.",
user: @user,
)

@note2 = @project.notes.create(
message: "This is the second note.",
user: @user,
)

@note3 = @project.notes.create(
message: "First, preheat the oven.",
user: @user,
)
end

# 一致するデータが見つかるとき
context "when a match is found" do
# 検索文字列に一致するメモを返すこと
it "returns notes that match the search term" do
expect(Note.search("first")).to include(@note1, @note3)
end
end

# 一致するデータが1件も見つからないとき
context "when no match is found" do
# 空のコレクションを返すこと
it "returns an empty collection" do
expect(Note.search("message")).to be_empty
end
end
end

=begin
# 検索文字列に一致するメモを返すこと
it "returns notes that match the search term" do
user = User.create(
first_name: "Joe",
last_name: "Tester",
email: "joetester@example.com",
password: "dottle-nouveau-pavilion-tights-furze",
)

project = user.projects.create(
name: "Test Project",
)

note1 = project.notes.create(
message: "This is the first note.",
user: user,
)

note2 = project.notes.create(
message: "This is the second note.",
user: user,
)

note3 = project.notes.create(
message: "First, preheat the oven.",
user: user,
)

expect(Note.search("first")).to include(note1, note3)
expect(Note.search("first")).to_not include(note2)
end

# 検索結果を検証するスペック...

# 検索結果が1件も見つからなければ空のコレクションを返すこと
it "returns an empty collection when no results are found" do
user = User.create(
first_name: "Joe",
last_name: "Tester",
email: "joetester@example.com",
password: "dottle-nouveau-pavilion-tights-furze",
)

project = user.projects.create(
name: "Test Project",
)

note1 = project.notes.create(
message: "This is the first note.",
user: user,
)

note2 = project.notes.create(
message: "This is the second note.",
user: user,
)

note3 = project.notes.create(
message: "First, preheat the oven.",
user: user,
)

expect(Note.search("message")).to be_empty
end
=end
end
Loading