Skip to content

Commit

Permalink
Merge pull request #21 from npauzenga/tasks/controller-specs
Browse files Browse the repository at this point in the history
unit tests for users_controller
  • Loading branch information
npauzenga committed Nov 21, 2015
2 parents eed5800 + 40b724f commit 597079e
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 50 deletions.
70 changes: 35 additions & 35 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/npauzenga/faker.git
revision: 89b4206886bdc45bfde52a45e5d7481b9ceaef65
revision: 9e4bcaddcc3e91c0e30a59ac19d6228d140b9874
branch: adding-hipster
specs:
faker (1.5.0)
Expand Down Expand Up @@ -48,7 +48,7 @@ GEM
ast (2.1.0)
astrolabe (1.3.1)
parser (~> 2.2)
autoprefixer-rails (6.0.3)
autoprefixer-rails (6.1.0.1)
execjs
json
bcrypt (3.1.10)
Expand All @@ -62,8 +62,7 @@ GEM
autoprefixer-rails (>= 5.0.0.1)
sass (>= 3.3.0)
builder (3.2.2)
byebug (5.0.0)
columnize (= 0.9.0)
byebug (8.2.0)
capybara (2.5.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
Expand All @@ -78,14 +77,14 @@ GEM
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.9.1.1)
columnize (0.9.0)
coveralls (0.8.3)
coffee-script-source (1.10.0)
coveralls (0.8.9)
json (~> 1.8)
rest-client (>= 1.6.8, < 2)
simplecov (~> 0.10.0)
term-ansicolor (~> 1.3)
thor (~> 0.19.1)
tins (~> 1.6.0)
debug_inspector (0.0.2)
diff-lcs (1.2.5)
docile (1.1.5)
Expand Down Expand Up @@ -133,7 +132,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
listen (3.0.3)
listen (3.0.4)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
loofah (2.0.3)
Expand All @@ -144,29 +143,29 @@ GEM
method_source (0.8.2)
mime-types (2.6.2)
mini_portile (0.6.2)
minitest (5.8.1)
minitest (5.8.2)
multi_json (1.11.2)
nenv (0.2.0)
netrc (0.10.3)
nokogiri (1.6.6.2)
netrc (0.11.0)
nokogiri (1.6.6.3)
mini_portile (~> 0.6.0)
notiffany (0.0.8)
nenv (~> 0.1)
shellany (~> 0.0)
parser (2.2.3.0)
ast (>= 1.1, < 3.0)
pg (0.18.3)
pg (0.18.4)
powerpack (0.1.1)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.2.0)
byebug (~> 5.0)
pry-byebug (3.3.0)
byebug (~> 8.0)
pry (~> 0.10)
pry-rails (0.3.4)
pry (>= 0.9.10)
puma (2.14.0)
puma (2.15.3)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -211,33 +210,34 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.0)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-rails (3.3.3)
rspec-support (~> 3.4.0)
rspec-rails (3.4.0)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
rubocop (0.34.2)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.0)
rubocop (0.35.1)
astrolabe (~> 1.3)
parser (>= 2.2.2.5, < 3.0)
parser (>= 2.2.3.0, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.4)
ruby-progressbar (~> 1.7)
tins (<= 1.6.0)
rubocop-rspec (1.3.1)
ruby-progressbar (1.7.5)
sass (3.4.19)
Expand All @@ -257,7 +257,7 @@ GEM
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
slop (3.6.0)
spring (1.4.0)
spring (1.4.2)
sprockets (3.4.0)
rack (> 1, < 3)
sprockets-rails (2.3.3)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def new
end

def create
result = CreateUser.call(user: @user, user_params: user_params)
result = CreateUser.call(user_params: user_params)

if result.success?
flash[:notice] = "Thanks! Please check your email to complete sign up"
Expand Down
8 changes: 0 additions & 8 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@
email_confirmed: true
)

nate = User.create(
name: "Nate",
email: "plabass@gmail.com",
password: "helloworld",
password_confirmation: "helloworld",
email_confirmed: true
)

50.times do
Todo.create!(
description: Faker::Hipster.sentence,
Expand Down
120 changes: 115 additions & 5 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,121 @@
RSpec.describe UsersController, type: :controller do
let(:user) { create(:unconfirmed_user) }

describe "email confirmation" do
it "sends a confirmation email when user registers" do
expect {
UserMailer.registration_confirmation(user).deliver_now
}.to change(ActionMailer::Base.deliveries, :size).by(1)
describe "GET #new" do
context "when successful" do
it "assigns new User to @user" do
get :new
expect(assigns(:user)).to be_a_new(User)
end

it "returns http status 200" do
get :new
expect(response).to have_http_status(200)
end

it "renders the #new template" do
get :new
expect(response).to render_template :new
end
end
end

describe "POST #create" do
let(:params) { { user: interactor_input.fetch(:user_params) } }

let(:interactor_input) do
{ user_params: {
name: user.name,
email: user.email,
password: user.password,
password_confirmation: user.password_confirmation }
}
end

let(:interactor_context) do
Interactor::Context.new(errors: :val, user: user)
end

before(:example) do
allow(CreateUser).to receive(:call).with(interactor_input)
.and_return(interactor_context)
end

context "when valid params are given" do
it "calls the CreateUser interactor" do
expect(CreateUser).to receive(:call)
post :create, params
end

it "returns http status 302" do
post :create, params
expect(response).to have_http_status(302)
end

it "redirects to sign in" do
expect(post :create, params).to redirect_to(sign_in_path)
end
end

context "when invalid invalid params are passed" do
before(:example) do
allow(interactor_context).to receive(:success?) { false }
end

it "redirects to sign up" do
expect(post :create, params).to redirect_to(sign_up_path)
end
end
end

describe "GET #show" do
let(:confirmed_user_1) { create(:confirmed_user) }
let(:current_user) { create(:confirmed_user) }
let(:params) { { id: confirmed_user_1.id } }
let(:session) { { user_id: current_user.id } }

let(:arguments) do
{ id: params.fetch(:id).to_s,
current_id: session.fetch(:user_id) }
end

let(:context) { double(:context, success?: true, user: confirmed_user_1) }

before(:example) do
allow(ShowUser).to receive(:call).with(arguments)
.and_return(context)
end

context "when user logged in" do
it "calls the ShowUser interactor" do
expect(ShowUser).to receive(:call)
get :show, params, session
end

it "assigns valid User to @user" do
get :show, params, session
expect(assigns(:user)).to be(confirmed_user_1)
end

it "returns http status 200" do
get :show, params, session
expect(response).to have_http_status(200)
end

it "renders the #show view" do
get :show, params, session
expect(response).to render_template :show
end
end

context "when user not logged in" do
before do
session[:user_id] = nil
end

it "redirects to sign in" do
expect(get :show, params, session).to redirect_to(sign_in_path)
end
end
end
end
14 changes: 13 additions & 1 deletion spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
FactoryGirl.define do
factory :user do
factory :confirmed_user, class: User do
name "Abraham Lincoln"
sequence(:email, 100) { |n| "user#{n}@example.com" }
password "helloworld"
password_confirmation "helloworld"
email_confirmed true
after(:create) { |user| user.confirm_token = nil }
after(:create) { |user| user.confirm_digest = nil }
after(:create) { |user| EncryptPassword.call(user: user) }
end

factory :unconfirmed_user, class: User do
Expand All @@ -14,5 +16,15 @@
password "helloworld"
password_confirmation "helloworld"
email_confirmed false

after(:create) do |user|
user.confirm_token = SecureRandom.urlsafe_base64.to_s
end
after(:create) do |user|
user.confirm_digest = Encryptor.digest_token(user.confirm_token)
end
after(:create) do |user|
EncryptPassword.call(user: user)
end
end
end

0 comments on commit 597079e

Please sign in to comment.