Skip to content

Commit

Permalink
Add test for ConnsController
Browse files Browse the repository at this point in the history
  • Loading branch information
yamanetoshi committed Sep 8, 2013
1 parent 1df3bd0 commit 151e695
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 7 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ gem 'twitter-bootstrap-rails'
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :test do
gem 'factory_girl_rails', '4.1.0'
gem 'rspec'
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ GEM
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (2.0.1)
factory_girl (4.1.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.1.0)
factory_girl (~> 4.1.0)
railties (>= 3.0.0)
hike (1.2.3)
i18n (0.6.1)
journey (1.0.4)
Expand Down Expand Up @@ -146,10 +151,12 @@ DEPENDENCIES
bootstrap-sass (= 2.1)
coffee-rails (~> 3.2.1)
devise
factory_girl_rails (= 4.1.0)
jquery-rails (= 2.0.2)
less-rails
pg (= 0.12.2)
rails (= 3.2.13)
rspec
rspec-rails (= 2.11.0)
sass-rails (~> 3.2.3)
sqlite3 (= 1.3.5)
Expand Down
21 changes: 14 additions & 7 deletions app/controllers/conns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class ConnsController < ApplicationController
# GET /conns
# GET /conns.json
def index
@conns = Conn.all
# @conns = Conn.all
@conns = current_user.conns

respond_to do |format|
format.html # index.html.erb
Expand All @@ -14,7 +15,8 @@ def index
# GET /conns/1
# GET /conns/1.json
def show
@conn = Conn.find(params[:id])
# @conn = Conn.find(params[:id])
@conn = current_user.conns.find(params[:id])

respond_to do |format|
format.html # show.html.erb
Expand All @@ -25,7 +27,8 @@ def show
# GET /conns/new
# GET /conns/new.json
def new
@conn = Conn.new
# @conn = Conn.new
@conn = current_user.conns.build

respond_to do |format|
format.html # new.html.erb
Expand All @@ -35,13 +38,15 @@ def new

# GET /conns/1/edit
def edit
@conn = Conn.find(params[:id])
# @conn = Conn.find(params[:id])
@conn = current_user.conns.find(params[:id])
end

# POST /conns
# POST /conns.json
def create
@conn = Conn.new(params[:conn])
# @conn = Conn.new(params[:conn])
@conn = current_user.conns.build(params[:conn])

respond_to do |format|
if @conn.save
Expand All @@ -57,7 +62,8 @@ def create
# PUT /conns/1
# PUT /conns/1.json
def update
@conn = Conn.find(params[:id])
# @conn = Conn.find(params[:id])
@conn = current_user.conns.find(params[:id])

respond_to do |format|
if @conn.update_attributes(params[:conn])
Expand All @@ -73,7 +79,8 @@ def update
# DELETE /conns/1
# DELETE /conns/1.json
def destroy
@conn = Conn.find(params[:id])
# @conn = Conn.find(params[:id])
@conn = current_user.conns.find(params[:id])
@conn.destroy

respond_to do |format|
Expand Down
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@

# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end
262 changes: 262 additions & 0 deletions spec/controllers/conns_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
require 'spec_helper'

describe ConnsController do
describe "Get #index" do
context "When user is not signed in" do
it "should redirect to signin screen" do
get :index
expect(response).to redirect_to(new_user_session_path)
end
end

context "When user is signed in" do
before do
login_user
end

it "responds successfully with on HTTP 200 status code" do
get :index
expect(response).to be_success
expect(response.code).to eq("200")
end

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

after do
sign_out :user
end
end
end

describe "Get #new" do
context "When user is not signed in" do
it "should redirect to signin screen" do
get :index
expect(response).to redirect_to(new_user_session_path)
end
end

context "When user is signed in" do
before do
login_user
end

it "responds successfully with on HTTP 200 status code" do
get :new
expect(response).to be_success
expect(response.code).to eq("200")
end

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

after do
sign_out :user
end
end
end

describe "Get #show" do
context "When user is not signed in" do
it "should redirect to signin screen" do
get :show, {:id => 1}
expect(response).to redirect_to(new_user_session_path)
end
end

context "When user is signed in" do
before do
login_user
conn = FactoryGirl.create(:conn)
end

it "responds successfully with on HTTP 200 status code" do
get :show, {:id => 1 }
expect(response).to be_success
expect(response.code).to eq("200")
end

it "renders the index template" do
get :show, {:id => 1}
expect(response).to render_template("show")
end

after do
sign_out :user
end
end
end

describe "Get #edit" do
context "When user is not signed in" do
it "should redirect to signin screen" do
get :edit, {:id => 1}
expect(response).to redirect_to(new_user_session_path)
end
end

context "When user is signed in" do
before do
login_user
conn = FactoryGirl.create(:conn)
end

it "responds successfully with on HTTP 200 status code" do
get :edit, {:id => 1 }
expect(response).to be_success
expect(response.code).to eq("200")
end

it "renders the index template" do
get :edit, {:id => 1}
expect(response).to render_template("edit")
end

after do
sign_out :user
end
end
end

describe "Post #create" do
context "When user is not signed in" do
it "should redirect to signin screen" do
post :create
expect(response).to redirect_to(new_user_session_path)
end
end

context "When user is signed in" do
before do
login_user
end

context "with valid params" do
it "creates a new item" do
expect {
post :create, {:conn => {}}}.to change(Conn, :count).by(1)
end

it "assigns a newly created item as @item" do
post :create, {:conn => {}}
expect(assigns(:conn)).to be_a(Conn)
expect(assigns(:conn)).to be_persisted
end

it "redirects to the created item" do
post :create, {:conn => {}}
expect(response).to redirect_to(Conn.last)
end
end

context "with invalid params" do
it "assigns a newly created but unsaved item as @item" do
Conn.any_instance.stub(:save).and_return(false)
post :create, {:conn => {}}
expect(assigns(:conn)).to be_a_new(Conn)
end

it "re-renders the 'new' template" do
Conn.any_instance.stub(:save).and_return(false)
post :create, {:conn => {}}
expect(response).to render_template("new")
end
end

after do
sign_out :user
end
end
end

describe "Put #update" do
context "When user is not signed in" do
it "should redirect to signin screen" do
put :update, {:id => 1, :conn => {}}
expect(response).to redirect_to(new_user_session_path)
end
end

context "When user is signed in" do
before do
login_user
end

context "with valid params" do
it "updates the requested conn" do
conn = FactoryGirl.create(:conn)
Conn.any_instance.should_receive(:update_attributes).with({'access_key' => 'params'})
put :update, {:id => conn.to_param, :conn => {:access_key => 'params'}}
end

it "assigns the requested conn as @conn" do
conn = FactoryGirl.create(:conn)
put :update, {:id => conn.to_param, :conn => {}}
expect(assigns(:conn)).to eq(conn)
end

it "redirects to the item" do
conn = FactoryGirl.create(:conn)
put :update, {:id => conn.to_param, :conn => {}}
expect(response).to redirect_to(conn)
end
end

context "with invalid params" do
it "assigns the conn as @conn" do
conn = FactoryGirl.create(:conn)
Conn.any_instance.stub(:save).and_return(false)
put :update, {:id => conn.to_param, :conn => {}}
expect(assigns(:conn)).to eq(conn)
end

it "re-renders the 'edit' template" do
conn = FactoryGirl.create(:conn)
Conn.any_instance.stub(:save).and_return(false)
put :update, {:id => conn.to_param, :conn => {}}
expect(response).to render_template("edit")
end
end

after do
sign_out :user
end
end
end


describe "Delete #destroy" do
context "When user is not signed in" do
it "should redirect to signin screen" do
delete :destroy, {:id => 1, :conn => {}}
expect(response).to redirect_to(new_user_session_path)
end
end

context "When user is signed in" do
before do
login_user
end

it "destroys the requested item" do
conn = FactoryGirl.create(:conn)
expect {
delete :destroy, {:id => conn.to_param}
}.to change(Conn, :count).by(-1)
end

it "redirects to the items list" do
conn = FactoryGirl.create(:conn)
delete :destroy, {:id => conn.to_param}
expect(response).to redirect_to(conns_url)
end

end
end

end
13 changes: 13 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FactoryGirl.define do
factory :user do
id 1
email "michael@example.com"
password "foobarbaz"
password_confirmation "foobarbaz"
end

factory :conn do
id 1
user_id 1
end
end
Loading

0 comments on commit 151e695

Please sign in to comment.