You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
As for contacts_controller_spec.rb, I found many points to be refactored.
Narrow the scope of let
Use let instead of instance variables for consistency
Delete useless stubbing
Use assigns() instead of assings[] for consistency
Use and_return method instead of block for readability
I would be glad if you would consider to apply these changes.
Here is diff:
diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb
index 501380c..a047d68 100644
--- a/spec/controllers/contacts_controller_spec.rb+++ b/spec/controllers/contacts_controller_spec.rb@@ -1,23 +1,6 @@
require 'rails_helper'
describe ContactsController do
- let(:admin) { build_stubbed(:admin) }- let(:user) { build_stubbed(:user) }-- let(:contact) do- create(:contact, firstname: 'Lawrence', lastname: 'Smith')- end-- let(:phones) do- [- attributes_for(:phone, phone_type: "home"),- attributes_for(:phone, phone_type: "office"),- attributes_for(:phone, phone_type: "mobile")- ]- end-- let(:valid_attributes) { attributes_for(:contact) }- let(:invalid_attributes) { attributes_for(:invalid_contact) }
shared_examples_for 'public access to contacts' do
describe 'GET #index' do
@@ -55,12 +38,8 @@ describe ContactsController do
firstname: 'Lawrence', lastname: 'Smith') }
before :each do
- allow(Contact).to receive(:persisted?).and_return(true)- allow(Contact).to \- receive(:order).with('lastname, firstname').and_return([contact])
allow(Contact).to \
receive(:find).with(contact.id.to_s).and_return(contact)
- allow(Contact).to receive(:save).and_return(true)
get :show, id: contact
end
@@ -111,8 +90,8 @@ describe ContactsController do
end
describe "POST #create" do
- before :each do- @phones = [+ let(:phones) do+ [
attributes_for(:phone),
attributes_for(:phone),
attributes_for(:phone)
@@ -123,15 +102,15 @@ describe ContactsController do
it "saves the new contact in the database" do
expect{
post :create, contact: attributes_for(:contact,
- phones_attributes: @phones)+ phones_attributes: phones)
}.to change(Contact, :count).by(1)
end
it "redirects to contacts#show" do
post :create,
contact: attributes_for(:contact,
- phones_attributes: @phones)- expect(response).to redirect_to contact_path(assigns[:contact])+ phones_attributes: phones)+ expect(response).to redirect_to contact_path(assigns(:contact))
end
end
@@ -152,42 +131,47 @@ describe ContactsController do
end
describe 'PATCH #update' do
+ let(:contact) do+ create(:contact, firstname: 'Lawrence', lastname: 'Smith')+ end+
before :each do
- @contact = create(:contact,- firstname: 'Lawrence',- lastname: 'Smith'- )+ contact # let!(:contact) is also okay
end
context "valid attributes" do
+ let(:valid_attributes) { attributes_for(:contact) }+
it "locates the requested @contact" do
allow(contact).to \
- receive(:update).with(valid_attributes.stringify_keys) { true }- patch :update, id: @contact,+ receive(:update).with(valid_attributes.stringify_keys).and_return(true)+ patch :update, id: contact,
contact: attributes_for(:contact)
- expect(assigns(:contact)).to eq @contact+ expect(assigns(:contact)).to eq contact
end
it "changes the contact's attributes" do
- patch :update, id: @contact,+ patch :update, id: contact,
contact: attributes_for(:contact,
firstname: 'Larry',
lastname: 'Smith'
)
- @contact.reload- expect(@contact.firstname).to eq 'Larry'- expect(@contact.lastname).to eq 'Smith'+ contact.reload+ expect(contact.firstname).to eq 'Larry'+ expect(contact.lastname).to eq 'Smith'
end
it "redirects to the updated contact" do
- patch :update, id: @contact, contact: attributes_for(:contact)- expect(response).to redirect_to @contact+ patch :update, id: contact, contact: attributes_for(:contact)+ expect(response).to redirect_to contact
end
end
context "invalid attributes" do
+ let(:invalid_attributes) { attributes_for(:invalid_contact) }+
before :each do
- allow(contact).to receive(:update).with(invalid_attributes.stringify_keys) { false }+ allow(contact).to receive(:update).with(invalid_attributes.stringify_keys).and_return(false)
patch :update, id: contact, contact: invalid_attributes
end
@@ -206,25 +190,26 @@ describe ContactsController do
end
describe 'DELETE #destroy' do
- before :each do- @contact = create(:contact)- end+ let(:contact) { create(:contact) }
it "deletes the contact" do
contact
expect{
delete :destroy, id: contact
- }.to change(Contact,:count).by(-1)+ }.to change(Contact, :count).by(-1)
end
it "redirects to contacts#index" do
- delete :destroy, id: @contact+ contact+ delete :destroy, id: contact
expect(response).to redirect_to contacts_url
end
end
end
describe "administrator access" do
+ let(:admin) { build_stubbed(:admin) }+
before :each do
allow(controller).to receive(:current_user).and_return(admin)
end
@@ -234,6 +219,8 @@ describe ContactsController do
end
describe "user access" do
+ let(:user) { build_stubbed(:user) }+
before :each do
allow(controller).to receive(:current_user).and_return(user)
end
My contacts_controller_spec is like this:
require'rails_helper'describeContactsControllerdoshared_examples_for'public access to contacts'dodescribe'GET #index'docontext'with params[:letter]'doit"populates an array of contacts starting with the letter"dosmith=create(:contact,lastname: 'Smith')jones=create(:contact,lastname: 'Jones')get:index,letter: 'S'expect(assigns(:contacts)).tomatch_array([smith])endit"renders the :index template"doget:index,letter: 'S'expect(response).torender_template:indexendendcontext'without params[:letter]'doit"populates an array of all contacts"dosmith=create(:contact,lastname: 'Smith')jones=create(:contact,lastname: 'Jones')get:indexexpect(assigns(:contacts)).tomatch_array([smith,jones])endit"renders the :index template"doget:indexexpect(response).torender_template:indexendendenddescribe'GET #show'dolet(:contact){build_stubbed(:contact,firstname: 'Lawrence',lastname: 'Smith')}before:eachdoallow(Contact).to \
receive(:find).with(contact.id.to_s).and_return(contact)get:show,id: contactendit"assigns the requested contact to @contact"doexpect(assigns(:contact)).toeqcontactendit"renders the :show template"doexpect(response).torender_template:showendendendshared_examples'full access to contacts'dodescribe'GET #new'doit"assigns a new Contact to @contact"doget:newexpect(assigns(:contact)).tobe_a_new(Contact)endit"assigns a home, office, and mobile phone to the new contact"doget:newphones=assigns(:contact).phones.mapdo |p|
p.phone_typeendexpect(phones).tomatch_array%w(homeofficemobile)endit"renders the :new template"doget:newexpect(response).torender_template:newendenddescribe'GET #edit'doit"assigns the requested contact to @contact"docontact=create(:contact)get:edit,id: contactexpect(assigns(:contact)).toeqcontactendit"renders the :edit template"docontact=create(:contact)get:edit,id: contactexpect(response).torender_template:editendenddescribe"POST #create"dolet(:phones)do[attributes_for(:phone),attributes_for(:phone),attributes_for(:phone)]endcontext"with valid attributes"doit"saves the new contact in the database"doexpect{post:create,contact: attributes_for(:contact,phones_attributes: phones)}.tochange(Contact,:count).by(1)endit"redirects to contacts#show"dopost:create,contact: attributes_for(:contact,phones_attributes: phones)expect(response).toredirect_tocontact_path(assigns(:contact))endendcontext"with invalid attributes"doit"does not save the new contact in the database"doexpect{post:create,contact: attributes_for(:invalid_contact)}.not_tochange(Contact,:count)endit"re-renders the :new template"dopost:create,contact: attributes_for(:invalid_contact)expect(response).torender_template:newendendenddescribe'PATCH #update'dolet(:contact)docreate(:contact,firstname: 'Lawrence',lastname: 'Smith')endbefore:eachdocontact# let!(:contact) is also okayendcontext"valid attributes"dolet(:valid_attributes){attributes_for(:contact)}it"locates the requested @contact"doallow(contact).to \
receive(:update).with(valid_attributes.stringify_keys).and_return(true)patch:update,id: contact,contact: attributes_for(:contact)expect(assigns(:contact)).toeqcontactendit"changes the contact's attributes"dopatch:update,id: contact,contact: attributes_for(:contact,firstname: 'Larry',lastname: 'Smith')contact.reloadexpect(contact.firstname).toeq'Larry'expect(contact.lastname).toeq'Smith'endit"redirects to the updated contact"dopatch:update,id: contact,contact: attributes_for(:contact)expect(response).toredirect_tocontactendendcontext"invalid attributes"dolet(:invalid_attributes){attributes_for(:invalid_contact)}before:eachdoallow(contact).toreceive(:update).with(invalid_attributes.stringify_keys).and_return(false)patch:update,id: contact,contact: invalid_attributesendit"locates the requested @contact"doexpect(assigns(:contact)).toeqcontactendit"does not change the contact's attributes"doexpect(assigns(:contact).reload.attributes).toeqcontact.attributesendit"re-renders the edit method"doexpect(response).torender_template:editendendenddescribe'DELETE #destroy'dolet(:contact){create(:contact)}it"deletes the contact"docontactexpect{delete:destroy,id: contact}.tochange(Contact,:count).by(-1)endit"redirects to contacts#index"docontactdelete:destroy,id: contactexpect(response).toredirect_tocontacts_urlendendenddescribe"administrator access"dolet(:admin){build_stubbed(:admin)}before:eachdoallow(controller).toreceive(:current_user).and_return(admin)endit_behaves_like'public access to contacts'it_behaves_like'full access to contacts'enddescribe"user access"dolet(:user){build_stubbed(:user)}before:eachdoallow(controller).toreceive(:current_user).and_return(user)endit_behaves_like'public access to contacts'it_behaves_like'full access to contacts'enddescribe"guest access"doit_behaves_like'public access to contacts'describe'GET #new'doit"requires login"doget:newexpect(response).torequire_loginendenddescribe'GET #edit'doit"requires login"docontact=create(:contact)get:edit,id: contactexpect(response).torequire_loginendenddescribe"POST #create"doit"requires login"dopost:create,id: create(:contact),contact: attributes_for(:contact)expect(response).torequire_loginendenddescribe'PUT #update'doit"requires login"doput:update,id: create(:contact),contact: attributes_for(:contact)expect(response).torequire_loginendenddescribe'DELETE #destroy'doit"requires login"dodelete:destroy,id: create(:contact)expect(response).torequire_loginendendendend
The text was updated successfully, but these errors were encountered:
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
As for contacts_controller_spec.rb, I found many points to be refactored.
let
let
instead of instance variables for consistencyassigns()
instead ofassings[]
for consistencyand_return
method instead of block for readabilityI would be glad if you would consider to apply these changes.
Here is diff:
My contacts_controller_spec is like this:
The text was updated successfully, but these errors were encountered: