From 8c1e430bb22877321ff8d0bb5b6687aec86073a2 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Mon, 31 Aug 2015 18:03:21 +0200 Subject: [PATCH] AutoCommit Mon Aug 31 18:03:21 CEST 2015 --- .../fetprofiles_controller_spec.rb | 105 +++++++----------- spec/controllers/shared_examples/basic.rb | 42 +++++++ spec/models/fetprofile_spec.rb | 5 + 3 files changed, 89 insertions(+), 63 deletions(-) create mode 100644 spec/controllers/shared_examples/basic.rb diff --git a/spec/controllers/fetprofiles_controller_spec.rb b/spec/controllers/fetprofiles_controller_spec.rb index c7bd94c..46607d3 100644 --- a/spec/controllers/fetprofiles_controller_spec.rb +++ b/spec/controllers/fetprofiles_controller_spec.rb @@ -3,38 +3,6 @@ require 'spec_helper' describe FetprofilesController, :type => :controller do let(:valid_update_attributes) { {"vorname"=>"Neuer Vorname"} } # let(:valid_session) { {locale: :de} } - shared_examples "it is success" do - it "has a 200 status code" do - action - expect(response.status).to eq(200) - end - it "is success" do - action - expect(response).to be_success - end - 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(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 def self.create_fetprofile before(:each) do @@ -68,6 +36,10 @@ describe FetprofilesController, :type => :controller do it "renders the 'show' template" do expect(action).to render_template("show") end + it "assigns memberships" do + action + expect(assigns(:memberships)).to be_a(ActiveRecord::Relation) + end end @@ -82,10 +54,7 @@ describe FetprofilesController, :type => :controller 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 @@ -95,6 +64,10 @@ describe FetprofilesController, :type => :controller do create_fetprofile it_behaves_like "it is success" it_behaves_like "it assigns object" + it "assigns memberships" do + action + expect(assigns(:memberships)).to be_a(ActiveRecord::Relation) + end it "assigns the requested fetprofile as @fetprofile" do expect{action}.to change {assigns(:fetprofile)}.to(@fetprofile) @@ -108,35 +81,36 @@ describe FetprofilesController, :type => :controller do end describe "POST create" do - subject(:action){post :create, {:fetprofile => FactoryGirl.attributes_for(:fetprofile)},:format=>:html} + subject(:action){post :create, {:fetprofile => FactoryGirl.attributes_for(:fetprofile),:format=>:html}} login_fet_user describe "with valid params" do -# login_fet_user - it "uses valid params" do - expect(Fetprofile.new({:vorname => "sdfsdf", :nachname=>"sdfefw",:desc=>"sdfsdfe"}).save).to be true - end -# before(:each) do -# Fetprofile.any_instance.stub(:save).and_return(true) -# end + it "has a 302 status code" do + expect(action.status).to eq(302) + end it "creates a new Fetprofile" do expect{action}.to change(Fetprofile, :count).by(1) end - it "assigns a newly created fetprofile as @fetprofile" do + it_behaves_like "it assigns persisted object" + + it "assigns memberships" do action - expect(assigns(:fetprofile)).to be_a(Fetprofile) - expect(assigns(:fetprofile)).to be_persisted + expect(assigns(:memberships)).to be_a(ActiveRecord::Relation) end - + it "redirects to the created fetprofile" do - expect(action).to redirect_to action: "show" + expect(action).to redirect_to assigns(:fetprofile) end end describe "with invalid params" do has_invalid_params + it "assigns memberships" do + action + expect(assigns(:memberships)).to be_a(ActiveRecord::Relation) + end it "assigns a newly created but unsaved fetprofile as @fetprofile" do action @@ -147,6 +121,11 @@ describe FetprofilesController, :type => :controller do expect(action).to render_template("new") end end + describe "without user" do + logout_user + it_behaves_like "it is restricted" + end + end describe "PUT update" do @@ -159,13 +138,9 @@ describe FetprofilesController, :type => :controller do 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 a created fetprofile as @fetprofile" do - action - expect(assigns(:fetprofile)).to be_a(Fetprofile) - expect(assigns(:fetprofile)).to be_persisted + expect { action }.to change{Fetprofile.find(@fetprofile.id).vorname}.to("neuerName") end + it_behaves_like "it assigns persisted object" it "redirects to the fetprofile" do expect(action).to redirect_to(@fetprofile) @@ -175,15 +150,14 @@ describe FetprofilesController, :type => :controller do describe "with invalid params" do has_invalid_params it_behaves_like "it assigns object" - - 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 - + it "re-renders the 'edit' template" do + expect(action).to render_template("edit") + end end + describe "without user" do + logout_user + it_behaves_like "it is restricted" + end end describe "DELETE destroy" do @@ -201,6 +175,11 @@ describe FetprofilesController, :type => :controller do action response.should redirect_to(fetprofiles_url) end + describe "without user" do + logout_user + it_behaves_like "it is restricted" + end + end end diff --git a/spec/controllers/shared_examples/basic.rb b/spec/controllers/shared_examples/basic.rb new file mode 100644 index 0000000..8da6780 --- /dev/null +++ b/spec/controllers/shared_examples/basic.rb @@ -0,0 +1,42 @@ + shared_examples "it is success" do + it "has a 200 status code" do + action + expect(response.status).to eq(200) + end + it "is success" do + action + expect(response).to be_success + end + 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(object_class) + expect(assigns(object_variable)).not_to be_persisted + + end + end + shared_examples "it assigns persisted object" do + it "assigns new object" do + action + expect(assigns(object_variable)).to be_a(object_class) + expect(assigns(object_variable)).to be_persisted + 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 diff --git a/spec/models/fetprofile_spec.rb b/spec/models/fetprofile_spec.rb index 958d3e3..73d44af 100644 --- a/spec/models/fetprofile_spec.rb +++ b/spec/models/fetprofile_spec.rb @@ -8,6 +8,11 @@ describe Fetprofile do fp.should_not be_valid end end + it "should be created" do + fp=Fetprofile.new(FactoryGirl.attributes_for(:fetprofile)) + expect(fp.save).to be true + end + it "should be valid" do fp = FactoryGirl.build(:fetprofile) fp.should be_valid