diff --git a/Gemfile b/Gemfile index c4efa84..37c1c53 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,7 @@ gem 'uglifier', '>= 1.3.0' group :development, :test do gem 'byebug', platform: :mri gem 'factory_girl_rails' + gem 'rails-controller-testing' gem 'rspec-rails' end diff --git a/Gemfile.lock b/Gemfile.lock index 3e47acb..90bbf4c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -171,6 +171,10 @@ GEM bundler (>= 1.3.0, < 2.0) railties (= 5.0.1) sprockets-rails (>= 2.0.0) + rails-controller-testing (1.0.1) + actionpack (~> 5.x) + actionview (~> 5.x) + activesupport (~> 5.x) rails-dom-testing (2.0.2) activesupport (>= 4.2.0, < 6.0) nokogiri (~> 1.6) @@ -311,6 +315,7 @@ DEPENDENCIES pg (~> 0.18) puma (~> 3.0) rails (~> 5.0.1) + rails-controller-testing rainbow (>= 2.1.0, < 2.2.0) reek rspec-rails diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 95ebf65..899ad93 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -1,7 +1,9 @@ class RoomsController < ApplicationController before_action :authenticate_user! - def index; end + def index + @rooms = Room.includes(:users).all + end def new @room = Room.new diff --git a/app/views/pages/user_has_no_room.html.slim b/app/views/pages/user_has_no_room.html.slim index 80964a4..d17e092 100644 --- a/app/views/pages/user_has_no_room.html.slim +++ b/app/views/pages/user_has_no_room.html.slim @@ -1,3 +1,5 @@ h1 Welcome to room538 - -= link_to 'Create new room', new_room_path +p + = link_to 'Create new room', new_room_path +p + = link_to 'All rooms', rooms_path diff --git a/app/views/rooms/_room.html.slim b/app/views/rooms/_room.html.slim new file mode 100644 index 0000000..e4ec948 --- /dev/null +++ b/app/views/rooms/_room.html.slim @@ -0,0 +1,4 @@ +.room + h3 = room.name + - room.users.each do |user| + p = user.name diff --git a/app/views/rooms/index.html.slim b/app/views/rooms/index.html.slim index 031a761..28a83cf 100644 --- a/app/views/rooms/index.html.slim +++ b/app/views/rooms/index.html.slim @@ -1,2 +1,2 @@ -h1 Rooms#index -p Find me in app/views/rooms/index.html.slim +h1 All rooms += render @rooms diff --git a/spec/controllers/rooms_controller_spec.rb b/spec/controllers/rooms_controller_spec.rb new file mode 100644 index 0000000..a8c783c --- /dev/null +++ b/spec/controllers/rooms_controller_spec.rb @@ -0,0 +1,22 @@ +RSpec.describe RoomsController, type: :controller do + include Devise::Test::ControllerHelpers + + context 'when user signed in' do + let(:user) { create(:user) } + before(:each) do + sign_in user + end + + describe 'GET #index' do + it 'returns http success' do + create_list(:room, 5, users: create_list(:user, 4)) + + get :index + + expect(response).to have_http_status(:success) + expect(response).to render_template(:index) + expect(assigns(:rooms).length).to eq 5 + end + end + end +end diff --git a/spec/factories/rooms.rb b/spec/factories/rooms.rb index ae7d14b..44f5dfd 100644 --- a/spec/factories/rooms.rb +++ b/spec/factories/rooms.rb @@ -1,5 +1,5 @@ FactoryGirl.define do factory :room do - name { Faker::Hipster.sentence(2) } + name { Faker::Hipster.word } end end diff --git a/spec/features/rooms_spec.rb b/spec/features/rooms_spec.rb index 28e60f9..bc38764 100644 --- a/spec/features/rooms_spec.rb +++ b/spec/features/rooms_spec.rb @@ -1,9 +1,12 @@ RSpec.feature 'Rooms' do let(:user) { create(:user) } - scenario 'User creates new room' do + background do sign_in user visit root_path + end + + scenario 'User creates new room' do click_link 'Create new room' fill_in 'Name', with: 'My room' @@ -11,4 +14,17 @@ expect(page).to have_content 'You successfully created new room "My room"' end + + scenario 'User looks at all rooms' do + rooms = create_list(:room, 3, users: create_list(:user, 3)) + + click_link 'All rooms' + + rooms.each do |room| + expect(page).to have_content room.name + room.users.each do |user| + expect(page).to have_content user.name + end + end + end end