Last active
October 28, 2020 23:51
-
-
Save minhquang4334/607c67fcff862d27bcf64101e544c94b to your computer and use it in GitHub Desktop.
Everyday-Rails 第6章 System Spec
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 "Events", js: true, type: :system do | |
scenario "user open event details page /events/:id" do | |
event = create(:event) | |
visit event_path(event) | |
expect(page).to have_selector("h1", text: event.name) | |
end | |
context "when user login in to app" do | |
include_context "when login required" | |
scenario "user create new event /events/new" do | |
# login_as(create(:user)) | |
visit new_event_path | |
expect(page).to have_selector("h1", text: "イベント作成") | |
fill_in "名前", with: "TokyuRubyKaigi" | |
fill_in "場所", with: "東京" | |
fill_in "内容", with: "tokyu.rbによる地域Ruby会議" | |
start_at = Time.zone.now | |
end_at = start_at + 1.day | |
start_at_field = "event_start_at" | |
select start_at.strftime("%Y"), from: "#{start_at_field}_1i" # 年 | |
select I18n.l(start_at, format: '%B'), from: "#{start_at_field}_2i" # 月 | |
select start_at.strftime("%-d"), from: "#{start_at_field}_3i" # 日 | |
select start_at.strftime("%H"), from: "#{start_at_field}_4i" # 時 | |
select start_at.strftime("%M"), from: "#{start_at_field}_5i" # 分 | |
end_at_field = "event_end_at" | |
select end_at.strftime("%Y"), from: "#{end_at_field}_1i" # 年 | |
select I18n.l(end_at, format: '%B'), from: "#{end_at_field}_2i" # 月 | |
select end_at.strftime("%-d"), from: "#{end_at_field}_3i" # 日 | |
select end_at.strftime("%H"), from: "#{end_at_field}_4i" # 時 | |
select end_at.strftime("%M"), from: "#{end_at_field}_5i" # 分 | |
click_button "登録する" | |
expect(page).to have_selector("div.alert", text: "作成しました") | |
end | |
scenario "user delete event /events/:id" do | |
event = create(:event, owner: user) | |
visit event_path(event) | |
expect { | |
accept_confirm do | |
click_on "イベントを削除する" | |
end | |
expect(current_path).to eq root_path | |
expect(page).to have_selector("div.alert", text: "削除しました") | |
expect(page).to_not have_selector("h5", text: event.name) | |
}.to change{Event.count}.by(-1) | |
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
def login_as(user) | |
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) | |
end | |
RSpec.shared_context 'when login required' do | |
let(:user) { create(:user) } | |
before do | |
login_as(user) | |
end | |
context 'when current_user is nil' do | |
before do | |
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(nil) | |
end | |
it 'depends on your spec' do | |
# actual spec goes here... | |
end | |
end | |
end | |
RSpec.configure do |config| | |
config.include_context 'when login required', :require_login | |
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
➜ awesome_events git:(section-4) ✗ bin/rspec | |
Running via Spring preloader in process 69498 | |
Event | |
Event Model attributes validation | |
when valid data | |
is valid with a name, place, content, start_at, end_at | |
#start_at_should_be_before_end_at valition | |
when start date < end date | |
start_at_should_be_before_end_at validation OK | |
when start date > end date | |
start_at_should_be_before_end_at validation errors | |
event can has many tickets | |
When number tickets are equal | |
can have many tickets | |
When number tickets are not equal | |
can have many tickets | |
Ticket | |
Model Tickets attributes validation | |
when valid data | |
is valid with a userId, eventId, comment | |
when invalid data | |
is invalid with a duplicate [user_id, event_id] | |
User | |
User Model attributes validation | |
when valid data | |
is valid with a name, provider, uid, and image_url | |
when invalid data | |
is invalid without a name | |
is invalid without a provider | |
is invalid without an uid | |
is invalid without an image_url | |
is invalid with a duplicate [provider, uid] | |
User Model Method validation | |
find or create from auth hash | |
find user from hash OK | |
create user from hash OK | |
Events | |
Capybara starting Puma... | |
* Version 4.3.6 , codename: Mysterious Traveller | |
* Min threads: 0, max threads: 4 | |
* Listening on tcp://127.0.0.1:61422 | |
user open event details page /events/:id | |
when user login in to app | |
user create new event /events/new | |
user delete event /events/:id | |
when current_user is nil | |
depends on your spec | |
Welcomes | |
open wellcome page can view title | |
open wellcome page can view only event in future | |
can view event with search keyword and from now | |
select time and view event after selected time | |
Finished in 15.78 seconds (files took 0.52017 seconds to load) | |
23 examples, 0 failures | |
Coverage report generated for RSpec to /Users/hoang.minh.quang/Documents/classi/kenshuu/fjord/awesome_events/coverage. 334 / 347 LOC (96.25%) covered. |
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 "Welcomes", type: :system do | |
scenario "open wellcome page can view title" do | |
visit "/" | |
expect(page).to have_selector("h1", text: "イベント一覧") | |
end | |
scenario "open wellcome page can view only event in future" do | |
visit root_path | |
future_event = create(:event, start_at: Time.zone.now + 3.day) | |
past_event = create(:event, start_at: Time.zone.now + 1.day) | |
Event.search_index.refresh | |
travel_to Time.zone.now + 2.day do | |
visit root_path | |
expect(page).to have_selector("h5", text: future_event.name) | |
expect(page).to_not have_selector("h5", text: past_event.name) | |
end | |
end | |
scenario "can view event with search keyword and from now" do | |
visit root_path | |
search_keyword = "Classi" | |
event_with_search_keyword = create(:event, start_at: Time.zone.now + 3.day, name: "123_Classi_123") | |
event_without_search_keyword = create(:event, start_at: Time.zone.now + 3.day, name: "shinjuku eki") | |
event_past_with_search_keyword = create(:event, start_at: Time.zone.now + 1.day, name: "shinjuku eki") | |
Event.search_index.refresh | |
travel_to Time.zone.now + 2.day do | |
visit root_path | |
fill_in "event_search_form[keyword]", with: search_keyword | |
click_button "検索" | |
expect(page).to have_selector("h5", text: event_with_search_keyword.name) | |
expect(page).to_not have_selector("h5", text: event_without_search_keyword.name) | |
expect(page).to_not have_selector("h5", text: event_past_with_search_keyword.name) | |
end | |
end | |
scenario "select time and view event after selected time" do | |
visit root_path | |
future_event = create(:event, start_at: Time.zone.now + 3.day) | |
past_event = create(:event, start_at: Time.zone.now - 1.day) | |
long_past_event = create(:event, start_at: Time.zone.now - 3.day) | |
Event.search_index.refresh | |
visit root_path | |
fill_in "event_search_form[start_at]", with: Time.zone.now - 2.day | |
click_button "検索" | |
expect(page).to have_selector("h5", text: future_event.name) | |
expect(page).to have_selector("h5", text: past_event.name) | |
expect(page).to_not have_selector("h5", text: long_past_event.name) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment