Skip to content
This repository was archived by the owner on Jul 21, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
03-models
  • Loading branch information
Tennnna committed Jan 19, 2019
commit 0cd49df8f2f3f133cd6554cbd37ac785de4421fa
142 changes: 142 additions & 0 deletions spec/models/note_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require 'rails_helper'

RSpec.describe Note, type: :model do

before do
# このファイルの全テストで使用するテストデータをセットアップする
@user = User.create(
first_name: "Joe",
last_name: "Tester",
email: "[email protected]",
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: "[email protected]",
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: "[email protected]",
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
51 changes: 51 additions & 0 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'rails_helper'

RSpec.describe Project, type: :model do
# ユーザー単位では重複したプロジェクト名を許可しないこと
it "does not allow duplicate project names per user" do
user = User.create(
first_name: "Joe",
last_name: "Tester",
email: "[email protected]",
password: "dottle-nouveau-pavilion-tights-furze",
)

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

new_project = user.projects.build(
name: "Test Project",
)

new_project.valid?
expect(new_project.errors[:name]).to include("has already been taken")
end

# 二人のユーザーが同じ名前を使うことは許可すること
it "allows two users to share a project name" do
user = User.create(
first_name: "Joe",
last_name: "Tester",
email: "[email protected]",
password: "dottle-nouveau-pavilion-tights-furze",
)

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

other_user = User.create(
first_name: "Jane",
last_name: "Tester",
email: "[email protected]",
password: "dottle-nouveau-pavilion-tights-furze",
)

other_project = other_user.projects.build(
name: "Test Project",
)

expect(other_project).to be_valid
end
end
61 changes: 61 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'rails_helper'

RSpec.describe User, type: :model do
# 姓、名、メール、パスワードがあれば有効な状態であること
it "is valid with a first name, last name, email, and password" do
user = User.new(
first_name: "Aaron",
last_name: "Summer",
email: "[email protected]",
password: "dottle-nouveau-tights-furze",
)
expect(user).to be_valid
end

# 名がなければ無効な状態であること
it "is invalid without a first name" do
user = User.new(first_name: nil)
user.valid?
expect(user.errors[:first_name]).to include("can't be blank")
# expect(user.errors[:first_name]).to_not include("can't be blank")
end

# 姓がなければ無効な状態であること
it "is invalid without a last name" do
user = User.new(last_name: nil)
user.valid?
expect(user.errors[:last_name]).to include("can't be blank")
end

# メールアドレスがなければ無効な状態であること
it "is invalid without an email address"

# 重複したメールアドレスなら無効な状態であること
it "is invalid with a duplicate email address" do
User.create(
first_name: "Joe",
last_name: "Tester",
email: "[email protected]",
password: "dottle-nouveau-pavilion-tights-furze",
)
user = User.new(
first_name: "Jane",
last_name: "Tester",
email: "[email protected]",
password: "dottle-nouveau-pavilion-tights-furze",
)
user.valid?
expect(user.errors[:email]).to include("has already been taken")
end

# ユーザーのフルネームを文字列として返すこと
it "returns a user's full name as a string" do
user = User.new(
first_name: "John",
last_name: "Doe",
email: "[email protected]",
)
expect(user.name).to eq "John Doe"
end
end