From 93dbdedf5348d9a2ac62929209fc465f436d723b Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Mon, 31 Aug 2015 16:03:01 +0200 Subject: [PATCH] AutoCommit Mon Aug 31 16:03:01 CEST 2015 --- .../fetprofiles_controller_spec.rb | 175 ++++++++---------- spec/factories/users.rb | 11 +- spec/support/controller_macros.rb | 24 +++ 3 files changed, 109 insertions(+), 101 deletions(-) diff --git a/spec/controllers/fetprofiles_controller_spec.rb b/spec/controllers/fetprofiles_controller_spec.rb index cfcf514..2d3dc7c 100644 --- a/spec/controllers/fetprofiles_controller_spec.rb +++ b/spec/controllers/fetprofiles_controller_spec.rb @@ -15,13 +15,24 @@ describe FetprofilesController, :type => :controller do end shared_examples "it assigns object" do it "assigns object variable" do + action expect(assigns(object_variable)).to eq(assigned_object_variable) end end shared_examples "it assigns new object" do it "assigns new object" do 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 @@ -30,83 +41,80 @@ describe FetprofilesController, :type => :controller do @fetprofile = FactoryGirl.create(:fetprofile) end end - describe "GET index" do - create_fetprofile - let(:action) { get :index, {} } + def self.create_active_fetprofile 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 "assigns all fetprofiles as @fetprofiles" do - @fetprofile = FactoryGirl.create(:fetprofile) - action - expect(assigns(:fetprofiles)).to eq(Fetprofile.active) - end + it_behaves_like "it assigns object" end describe "GET show" do - before(:each) do - end - subject(:action) { get :show, {:id => fetprofile.to_param}} - let(:object_variable) {:fetprofile} - let(:assigned_object_variable) {@fetprofile} + create_fetprofile + subject(:action) { get :show, {:id => @fetprofile.to_param}} + it_behaves_like "it is success" + it_behaves_like "it assigns object" end describe "GET new" do + subject(:action) { get :new, {}} describe "with fetuser" do 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 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 + it_behaves_like "it is success" end + describe "without user" do + logout_user + it_behaves_like "it is restricted" + end + # descibe "without fetuser" do + # login_user + # it_behaves_like "it is restricted" + # end end describe "GET edit" do - login_fet_user - before(:each) do - @fetprofile = FactoryGirl.create(:fetprofile) - end + 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 expect{action}.to change {assigns(:fetprofile)}.to(@fetprofile) # expect(assigns(:fetprofile)).to eq(@fetprofile) end - + describe "without user" do + logout_user + it_behaves_like "it is restricted" + end + end describe "POST create" do - let(:action){post :create, {:fetprofile => FactoryGirl.build(:fetprofile).attributes.slice(:vorname, :nachname,:desc)}} - -login_fet_user - + subject(:action){post :create, {:fetprofile => FactoryGirl.build(:fetprofile).attributes.slice(:vorname, :nachname,:desc)}} + login_fet_user describe "with valid params" do it "creates a new Fetprofile" do - - expect(action).to change(Fetprofile, :count).by(1) + expect{action}.to change(Fetprofile, :count).by(1) end it "assigns a newly created fetprofile as @fetprofile" do - action - assigns(:fetprofile).to be_a(Fetprofile) + action + expect(assigns(:fetprofile)).to be_a(Fetprofile) assigns(:fetprofile).should be_persisted end @@ -116,89 +124,62 @@ login_fet_user end describe "with invalid params" do - before(:each) do - Fetprofile.any_instance.stub(:save).and_return(false) - end + has_invalid_params + 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 + action assigns(:fetprofile).should be_a_new(Fetprofile) end - it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted - Fetprofile.any_instance.stub(:save).and_return(false) - post :create, {:fetprofile => { "vorname" => "invalid value" }}, valid_session - response.should render_template("new") + expect(action).to render_template("new") end end end describe "PUT update" do - before(:all) do - @fetprofile = FactoryGirl.create(:fetprofile) - end + let(:action) { put :update, {:id => @fetprofile.to_param, :fetprofile => {"vorname"=>"neuerName"}} } + + create_fetprofile login_fet_user 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 expect { action }.to change{Fetprofile.find(@fetprofile.id).vorname}#.to("neuerName") 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 - fetprofile = FactoryGirl.create(:fetprofile) - action - response.should redirect_to(fetprofile) + expect(action).to redirect_to(@fetprofile) end end 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 - Fetprofile.any_instance.stub(:save).and_return(false) - 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 + it "re-renders the 'edit' template" do + expect(action).to render_template("edit") + end +# it "redirect to 'edit'" do + # is_expected.to redirect_to action: "edit" + # end end end describe "DELETE destroy" do - before(:each) do - u= FactoryGirl.create(:user) - u.add_role(:fetuser) - sign_in u - end + login_fet_user + create_fetprofile + subject(:action) { delete :destroy, {:id => @fetprofile.to_param}} it "destroys the requested fetprofile" do - fetprofile = FactoryGirl.create(:fetprofile) expect { - delete :destroy, {:id => fetprofile.to_param}, valid_session + action }.to change(Fetprofile, :count).by(-1) end it "redirects to the fetprofiles list" do - fetprofile = FactoryGirl.create(:fetprofile) - delete :destroy, {:id => fetprofile.to_param}, valid_session + action response.should redirect_to(fetprofiles_url) end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 9cb2401..cc1ea5e 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -1,8 +1,11 @@ FactoryGirl.define do factory :user do - email "testuser@test.at" - password "password" - password_confirmation "password" - + email "testuser@test.at" + password "password" + password_confirmation "password" + factory :other_user do + email "othertest@test.at" + end end end + diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb index d439641..229a09a 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -11,4 +11,28 @@ module ControllerMacros sign_in user 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