Skip to content
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
69 changes: 69 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'rails_helper'

RSpec.describe UsersController, type: :controller do
describe '#index' do
it 'redirects to home page' do
get :index
expect(response).to redirect_to(root_path)
end
end

let(:user){ create(:user) }
describe '#create' do
context 'with valid params' do
def valid_user
post :create, params: { user: attributes_for(:user)}
end

it 'saves a record to the database' do
count_before = User.count
valid_user

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove space, lesson learned from Steve

count_after = User.count
expect(count_after).to eq(count_before + 1)
end

it 'saves session id of current user' do
# u = create(:user)
# expect(session[:user_id]).to eq(u.id)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment and space below it.

Probably better to set session[:user_id] to nil first and then do the comparison. I like the way you did in the comment though.

valid_user
expect(session[:user_id]).not_to eq(nil)
end
end

context 'with invalid params' do
def invalid_user
post :create, params: { user: attributes_for(:user, email: nil)}
end

it 'does not save a record to the database' do
count_before = User.count
invalid_user

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this space too >. <

count_after = User.count
expect(count_after).to eq(count_before)
end

it 'renders the new template' do
invalid_user
expect(response).to render_template(:new)
end
end
end

describe '#show' do
it 'renders the show template' do
user = create(:user)
get :show, params: { id: user.id }
expect(response).to render_template(:show)
end

it 'user id is passed' do
user = create(:user)
get :show, params: { id: user.id }
expect(assigns(:user)).to eq(user)
end
end

end
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
# instead of true.
config.use_transactional_fixtures = true

config.include FactoryGirl::Syntax::Methods

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
Expand Down