# frozen_string_literal: true require 'rails_helper' # This spec was generated by rspec-rails when you ran the scaffold generator. # It demonstrates how one might use RSpec to specify the controller code that # was generated by Rails when you ran the scaffold generator. # # It assumes that the implementation code is generated by the rails scaffold # generator. If you are using any extension libraries to generate different # controller code, this generated spec may or may not pass. # # It only uses APIs available in rails and/or rspec-rails. There are a number # of tools you can use to make these specs even more expressive, but we're # sticking to rails and rspec-rails APIs to keep things simple and stable. # # Compared to earlier versions of this generator, there is very limited use of # stubs and message expectations in this spec. Stubs are only used when there # is no simpler way to get a handle on the object needed for the example. # Message expectations are only used when there is no simpler way to specify # that an instance is receiving a specific message. RSpec.describe ProjectsController, type: :controller do let(:user) { FactoryGirl.create(:user) } let(:project) { FactoryGirl.create(:project, owner: user) } before { devise_user_login(user) } # This should return the minimal set of attributes required to create a valid # Project. As you add validations to Project, be sure to # adjust the attributes here as well. let(:valid_parameters) do FactoryGirl.attributes_for(:project).merge(owner_id: user.id) end let(:invalid_parameters) do valid_parameters.symbolize_keys.merge(name: '') end # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in # ProjectsController. Be sure to keep this updated too. let(:valid_session) { {} } describe 'GET #index' do it 'assigns all projects as @projects' do get :index, session: valid_session, params: {} expect(assigns(:projects)).to eq([project]) end end describe 'GET #show' do it 'assigns the requested project as @project' do project # To create project get :show, session: valid_session, params: { id: project.to_param } expect(assigns(:project)).to eq(project) end end describe 'GET #new' do it 'assigns a new project as @project' do get :new, session: valid_session, params: {} expect(assigns(:project)).to be_a_new(Project) end end describe 'GET #edit' do it 'assigns the requested project as @project' do project # To create project get :edit, session: valid_session, params: { id: project.to_param } expect(assigns(:project)).to eq(project) end end describe 'POST #create' do context 'with valid params' do it 'creates a new Project' do expect { post :create, session: valid_session, params: { project: valid_parameters } }.to change(Project, :count).by(1) end it 'assigns a newly created project as @project' do post :create, session: valid_session, params: { project: valid_parameters } expect(assigns(:project)).to be_a(Project) expect(assigns(:project)).to be_persisted end it 'redirects to the created project' do post :create, session: valid_session, params: { project: valid_parameters } expect(response).to redirect_to(Project.last) end end context 'with invalid params' do it 'assigns a newly created but unsaved project as @project' do post :create, session: valid_session, params: { project: invalid_parameters } expect(assigns(:project)).to be_a_new(Project) end it "re-renders the 'new' template" do post :create, session: valid_session, params: { project: invalid_parameters } expect(response).to render_template('new') end end end describe 'PUT #update' do context 'with valid params' do let(:new_name) { valid_parameters[:name].succ } let(:new_closed) { valid_parameters[:closed].! } let(:new_parameters) do valid_parameters.merge(name: new_name, closed: new_closed) end it 'updates the requested project' do project # To create project put :update, session: valid_session, params: { id: project.to_param, project: new_parameters } project.reload expect(project.name).to eq new_name expect(project.closed).to eq new_closed end it 'assigns the requested project as @project' do project # To create project put :update, session: valid_session, params: { id: project.to_param, project: new_parameters } expect(assigns(:project)).to eq(project) end it 'redirects to the project' do project # To create project put :update, session: valid_session, params: { id: project.to_param, project: new_parameters } expect(response).to redirect_to(project) end end context 'with invalid params' do it 'assigns the project as @project' do project # To create project put :update, session: valid_session, params: { id: project.to_param, project: invalid_parameters } expect(assigns(:project)).to eq(project) end it "re-renders the 'edit' template" do project # To create project put :update, session: valid_session, params: { id: project.to_param, project: invalid_parameters } expect(response).to render_template('edit') end end end describe 'DELETE #destroy' do it 'destroys the requested project' do project # To create project expect { delete :destroy, session: valid_session, params: { id: project.to_param } }.to change(Project, :count).by(-1) end it 'redirects to the projects list' do project # To create project delete :destroy, session: valid_session, params: { id: project.to_param } expect(response).to redirect_to(projects_url) end end end