Created
October 30, 2020 05:39
-
-
Save minhquang4334/f96e8473d0419d54263f5499b1eb0f62 to your computer and use it in GitHub Desktop.
第3章モデルスペック
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe Event, type: :model do | |
describe "Event Model attributes validation" do | |
context "when valid data" do | |
it "is valid with a name, place, content, start_at, end_at" do | |
user = create(:user, name: "Aaron Peter", provider: "Google", uid: "3456", image_url: "./quangdeptrai123.png") | |
new_event = create(:event, owner: user) | |
expect(new_event).to be_valid | |
end | |
end | |
end | |
describe "#start_at_should_be_before_end_at valition" do | |
context "when start date < end date" do | |
it "start_at_should_be_before_end_at validation OK" do | |
start_at = rand(1..30).days.from_now | |
end_at = start_at + rand(1..30).hours | |
event = build(:event, start_at: start_at, end_at: end_at) | |
event.valid? | |
expect(event.errors[:start_at]).to be_empty | |
end | |
end | |
context "when start date > end date" do | |
it "start_at_should_be_before_end_at validation errors" do | |
start_at = rand(1..30).days.from_now | |
end_at = start_at - rand(1..30).hours | |
event = build(:event, start_at: start_at, end_at: end_at) | |
event.valid? | |
expect(event.errors[:start_at]).to include("は終了時間よりも前に設定してください") | |
end | |
end | |
end | |
describe "event can has many tickets" do | |
context "When number tickets are equal" do | |
it "can have many tickets" do | |
event = create(:event, :with_tickets) | |
expect(event.tickets.length).to eq(100) | |
end | |
end | |
context "When number tickets are not equal" do | |
it "can have many tickets" do | |
event = create(:event, :with_tickets) | |
expect(event.tickets.length).to_not eq(10) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe Ticket, type: :model do | |
before do | |
@user = create(:user) | |
@event = create(:event) | |
end | |
describe "Model Tickets attributes validation" do | |
context "when valid data" do | |
it "is valid with a userId, eventId, comment" do | |
new_ticket = build(:ticket, user: @user, event: @event) | |
expect(new_ticket).to be_valid | |
end | |
end | |
context "when invalid data" do | |
subject(:ticket) { create(:ticket) } | |
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(:event_id) } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe User, type: :model do | |
describe "User Model attributes validation" do | |
context "when valid data" do | |
it "is valid with a name, provider, uid, and image_url" do | |
user = build(:user, name: "Aaron Peter", provider: "Google", uid: "3456", image_url: "./quangdeptrai123.png") | |
expect(user).to be_valid | |
end | |
end | |
context "when use shoulda matcher with valid data" do | |
it { is_expected.to validate_presence_of :name } | |
it { is_expected.to validate_presence_of :provider } | |
it { is_expected.to validate_presence_of :uid } | |
it { is_expected.to validate_presence_of :image_url } | |
subject(:user) { create(:user) } | |
it { is_expected.to validate_uniqueness_of(:provider).scoped_to(:uid) } | |
end | |
end | |
describe "User Model Method validation" do | |
context "find or create from auth hash" do | |
it "find user from hash OK" do | |
auth_hash = { | |
provider: "google_auth", | |
uid: "00test", | |
info: { | |
nickname: "quang_handsome", | |
image: "/hihi.png" | |
} | |
} | |
new_user = User.find_or_create_from_auth_hash!(auth_hash) | |
expect(new_user).to have_attributes(provider: "google_auth", uid: "00test", name: "quang_handsome", image_url: "/hihi.png") | |
end | |
it "create user from hash OK" do | |
auth_hash = { | |
provider: "google_auth_1", | |
uid: "00test_1", | |
info: { | |
nickname: "quang_handsome", | |
image: "/hihi.png" | |
} | |
} | |
new_user = User.find_or_create_from_auth_hash!(auth_hash) | |
expect(new_user).to have_attributes(provider: "google_auth_1", uid: "00test_1", name: "quang_handsome", image_url: "/hihi.png") | |
end | |
end | |
end | |
describe "testing with mock and stub" do | |
context "use mock" do | |
it "delegates name to the user who created it" do | |
user = double(:user, name: "Fake User") | |
event = Event.new | |
allow(event).to receive(:owner).and_return(user) | |
expect(event.owner.name).to eq "Fake User" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment