AutoCommit Mon Aug 31 16:03:01 CEST 2015

This commit is contained in:
Andreas Stephanides
2015-08-31 16:03:01 +02:00
parent 1bc9d65a44
commit 93dbdedf53
3 changed files with 109 additions and 101 deletions

View File

@@ -15,13 +15,24 @@ describe FetprofilesController, :type => :controller do
end end
shared_examples "it assigns object" do shared_examples "it assigns object" do
it "assigns object variable" do it "assigns object variable" do
action
expect(assigns(object_variable)).to eq(assigned_object_variable) expect(assigns(object_variable)).to eq(assigned_object_variable)
end end
end end
shared_examples "it assigns new object" do shared_examples "it assigns new object" do
it "assigns new object" do it "assigns new object" do
action action
expect(assigns(object_variable)).to be_a_new(assigned_object_variable) expect(assigns(object_variable)).to be_a_new(object_class)
end
end
shared_examples "it is restricted" do
it "is expected to raise error" do
bypass_rescue
expect { action }.to raise_error(CanCan::AccessDenied)
end
it "is not success" do
action
expect(response).not_to be_success
end end
end end
@@ -30,83 +41,80 @@ describe FetprofilesController, :type => :controller do
@fetprofile = FactoryGirl.create(:fetprofile) @fetprofile = FactoryGirl.create(:fetprofile)
end end
end end
describe "GET index" do def self.create_active_fetprofile
create_fetprofile
let(:action) { get :index, {} }
before(:each) do before(:each) do
@fetprofile = FactoryGirl.create(:fetprofile, active: true)
end
end
end let(:object_variable) {:fetprofile}
let(:assigned_object_variable) {@fetprofile}
let(:object_class) {Fetprofile}
describe "GET index" do
create_active_fetprofile
subject(:action) { get :index, {} }
let(:object_variable) {:fetprofiles}
let(:assigned_object_variable) {[@fetprofile]}
it_behaves_like "it is success" it_behaves_like "it is success"
it "assigns all fetprofiles as @fetprofiles" do it_behaves_like "it assigns object"
@fetprofile = FactoryGirl.create(:fetprofile)
action
expect(assigns(:fetprofiles)).to eq(Fetprofile.active)
end
end end
describe "GET show" do describe "GET show" do
before(:each) do create_fetprofile
end subject(:action) { get :show, {:id => @fetprofile.to_param}}
subject(:action) { get :show, {:id => fetprofile.to_param}} it_behaves_like "it is success"
let(:object_variable) {:fetprofile} it_behaves_like "it assigns object"
let(:assigned_object_variable) {@fetprofile}
end end
describe "GET new" do describe "GET new" do
subject(:action) { get :new, {}}
describe "with fetuser" do describe "with fetuser" do
login_fet_user login_fet_user
subject(:action) { get :new, {}}
let(:object_variable) {:fetprofile}
let(:assigned_object_variable) {Fetprofile}
it_behaves_like "it assigns new object" it_behaves_like "it assigns new object"
it_behaves_like "it is success" it_behaves_like "it is success"
end
describe "without fetuser" do
before(:each) do
sign_out :user
end
pending "doesn't assign @fetprofile"
it "is expected to raise error" do
bypass_rescue
expect { get :new }.to raise_error(CanCan::AccessDenied)
end
it "is not success" do
get :new, {}
expect(response).not_to be_success
end end
describe "without user" do
logout_user
it_behaves_like "it is restricted"
end end
# descibe "without fetuser" do
# login_user
# it_behaves_like "it is restricted"
# end
end end
describe "GET edit" do describe "GET edit" do
login_fet_user
before(:each) do
@fetprofile = FactoryGirl.create(:fetprofile)
end
subject(:action) {get :edit, :id=>@fetprofile.to_param} subject(:action) {get :edit, :id=>@fetprofile.to_param}
login_fet_user
create_fetprofile
it_behaves_like "it is success"
it_behaves_like "it assigns object"
it "assigns the requested fetprofile as @fetprofile" do it "assigns the requested fetprofile as @fetprofile" do
expect{action}.to change {assigns(:fetprofile)}.to(@fetprofile) expect{action}.to change {assigns(:fetprofile)}.to(@fetprofile)
# expect(assigns(:fetprofile)).to eq(@fetprofile) # expect(assigns(:fetprofile)).to eq(@fetprofile)
end end
describe "without user" do
logout_user
it_behaves_like "it is restricted"
end
end end
describe "POST create" do describe "POST create" do
let(:action){post :create, {:fetprofile => FactoryGirl.build(:fetprofile).attributes.slice(:vorname, :nachname,:desc)}} subject(:action){post :create, {:fetprofile => FactoryGirl.build(:fetprofile).attributes.slice(:vorname, :nachname,:desc)}}
login_fet_user
login_fet_user
describe "with valid params" do describe "with valid params" do
it "creates a new Fetprofile" do it "creates a new Fetprofile" do
expect{action}.to change(Fetprofile, :count).by(1)
expect(action).to change(Fetprofile, :count).by(1)
end end
it "assigns a newly created fetprofile as @fetprofile" do it "assigns a newly created fetprofile as @fetprofile" do
action action
assigns(:fetprofile).to be_a(Fetprofile) expect(assigns(:fetprofile)).to be_a(Fetprofile)
assigns(:fetprofile).should be_persisted assigns(:fetprofile).should be_persisted
end end
@@ -116,89 +124,62 @@ login_fet_user
end end
describe "with invalid params" do describe "with invalid params" do
before(:each) do has_invalid_params
Fetprofile.any_instance.stub(:save).and_return(false)
end
it "assigns a newly created but unsaved fetprofile as @fetprofile" do
# Trigger the behavior that occurs when invalid params are submitted
post :create, {:fetprofile => { "vorname" => "in", "nachname"=> "ssdf", "desc"=> "dffff" }}, valid_session it "assigns a newly created but unsaved fetprofile as @fetprofile" do
action
assigns(:fetprofile).should be_a_new(Fetprofile) assigns(:fetprofile).should be_a_new(Fetprofile)
end end
it "re-renders the 'new' template" do it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted expect(action).to render_template("new")
Fetprofile.any_instance.stub(:save).and_return(false)
post :create, {:fetprofile => { "vorname" => "invalid value" }}, valid_session
response.should render_template("new")
end end
end end
end end
describe "PUT update" do describe "PUT update" do
before(:all) do let(:action) { put :update, {:id => @fetprofile.to_param, :fetprofile => {"vorname"=>"neuerName"}} }
@fetprofile = FactoryGirl.create(:fetprofile)
end create_fetprofile
login_fet_user login_fet_user
describe "with valid params" do describe "with valid params" do
let(:action) { put :update, {:id => @fetprofile.to_param, :fetprofile => {"vorname"=>"neuerName"}} } it_behaves_like "it assigns object"
it "updates the requested fetprofile" do it "updates the requested fetprofile" do
expect { action }.to change{Fetprofile.find(@fetprofile.id).vorname}#.to("neuerName") expect { action }.to change{Fetprofile.find(@fetprofile.id).vorname}#.to("neuerName")
end end
it "assigns the requested fetprofile as @fetprofile" do
action
expect(assigns(:fetprofile)).to eq(Fetprofile.find(@fetprofile.to_param))
end
it "redirects to the fetprofile" do it "redirects to the fetprofile" do
fetprofile = FactoryGirl.create(:fetprofile) expect(action).to redirect_to(@fetprofile)
action
response.should redirect_to(fetprofile)
end end
end end
describe "with invalid params" do describe "with invalid params" do
let(:action) { put :update, {:id => @fetprofile.to_param, :fetprofile => {"vorname"=>"n"}} } has_invalid_params
it_behaves_like "it assigns object"
it "assigns the fetprofile as @fetprofile" do
Fetprofile.any_instance.stub(:save).and_return(false)
action
expect(assigns(:fetprofile)).to eq(@fetprofile)
end
it "re-renders the 'edit' template" do it "re-renders the 'edit' template" do
Fetprofile.any_instance.stub(:save).and_return(false) expect(action).to render_template("edit")
action
expect(response).to render_template("edit")
end
it "redirect to 'edit'" do
Fetprofile.any_instance.stub(:save).and_return(false)
expect(action).to redirect_to action: "edit"
end end
# it "redirect to 'edit'" do
# is_expected.to redirect_to action: "edit"
# end
end end
end end
describe "DELETE destroy" do describe "DELETE destroy" do
before(:each) do login_fet_user
u= FactoryGirl.create(:user) create_fetprofile
u.add_role(:fetuser) subject(:action) { delete :destroy, {:id => @fetprofile.to_param}}
sign_in u
end
it "destroys the requested fetprofile" do it "destroys the requested fetprofile" do
fetprofile = FactoryGirl.create(:fetprofile)
expect { expect {
delete :destroy, {:id => fetprofile.to_param}, valid_session action
}.to change(Fetprofile, :count).by(-1) }.to change(Fetprofile, :count).by(-1)
end end
it "redirects to the fetprofiles list" do it "redirects to the fetprofiles list" do
fetprofile = FactoryGirl.create(:fetprofile) action
delete :destroy, {:id => fetprofile.to_param}, valid_session
response.should redirect_to(fetprofiles_url) response.should redirect_to(fetprofiles_url)
end end
end end

View File

@@ -3,6 +3,9 @@ FactoryGirl.define do
email "testuser@test.at" email "testuser@test.at"
password "password" password "password"
password_confirmation "password" password_confirmation "password"
factory :other_user do
email "othertest@test.at"
end
end end
end end

View File

@@ -11,4 +11,28 @@ module ControllerMacros
sign_in user sign_in user
end end
end end
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.build(:other_user)
user = User.find_by_email(user.email) || user
user.save
# user.confirm!
sign_in user
end
end
def logout_user
before(:each) do
sign_out :user
end
end
def has_invalid_params
before(:each) do
object_class.any_instance.stub(:save).and_return(false)
object_class.any_instance.stub(:update_attributes).and_return(false)
end
end
end end