Created
September 8, 2013 09:51
-
-
Save yamanetoshi/6483432 to your computer and use it in GitHub Desktop.
devise な Controller の雛形とその試験
This file contains 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
class ConnsController < ApplicationController | |
before_filter :authenticate_user! | |
# GET /conns | |
# GET /conns.json | |
def index | |
@conns = current_user.conns | |
respond_to do |format| | |
format.html # index.html.erb | |
format.json { render json: @conns } | |
end | |
end | |
# GET /conns/1 | |
# GET /conns/1.json | |
def show | |
@conn = current_user.conns.find(params[:id]) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.json { render json: @conn } | |
end | |
end | |
# GET /conns/new | |
# GET /conns/new.json | |
def new | |
@conn = current_user.conns.build | |
respond_to do |format| | |
format.html # new.html.erb | |
format.json { render json: @conn } | |
end | |
end | |
# GET /conns/1/edit | |
def edit | |
@conn = current_user.conns.find(params[:id]) | |
end | |
# POST /conns | |
# POST /conns.json | |
def create | |
@conn = current_user.conns.build(params[:conn]) | |
respond_to do |format| | |
if @conn.save | |
format.html { redirect_to @conn, notice: 'Conn was successfully created.' } | |
format.json { render json: @conn, status: :created, location: @conn } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @conn.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /conns/1 | |
# PUT /conns/1.json | |
def update | |
@conn = current_user.conns.find(params[:id]) | |
respond_to do |format| | |
if @conn.update_attributes(params[:conn]) | |
format.html { redirect_to @conn, notice: 'Conn was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: "edit" } | |
format.json { render json: @conn.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /conns/1 | |
# DELETE /conns/1.json | |
def destroy | |
@conn = current_user.conns.find(params[:id]) | |
@conn.destroy | |
respond_to do |format| | |
format.html { redirect_to conns_url } | |
format.json { head :no_content } | |
end | |
end | |
end |
This file contains 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 '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 |
This file contains 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
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 |
This file contains 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
module ControllerMacros | |
def login_admin | |
@request.env["devise.mapping"] = Devise.mappings[:admin] | |
sign_in FactoryGirl.create(:admin) | |
end | |
def login_user | |
controller.stub(:authenticate_user!).and_return true | |
@request.env["devise.mapping"] = Devise.mappings[:user] | |
user = FactoryGirl.create(:user) | |
user.confirm! | |
sign_in user | |
end | |
end |
This file contains 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
RSpec.configure do |config| | |
config.include Devise::TestHelpers, :type => :controller | |
config.include ControllerMacros, :type => :controller | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment