Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize Rails RSpec Tests
    primarykey
    data
    text
    <p>I'm working on a test for my Rails 4 app and I'm pretty new to using RSpec. I have a controller named AppsController which has the standard index, new, show, create... methods and they all work the way Rails suggest Etc. "new" creates a new instance of the object and create actually saves it, show, shows it and index shows all of the object. Here are my current tests can anyone see any potential problems or things that i could improve?</p> <pre><code>FactoryGirl.define do factory :developer do email 'example@me.com' password 'new_york' password_confirmation 'new_york' tos '1' end factory :app do name 'New App' tos '1' end factory :invalid_app, parent: :app do name 'nil' tos '0' end end </code></pre> <hr> <pre><code>require 'spec_helper' def create_valid! post :create, app: app_attributes end def create_invalid! post :create, app: app_invalid_attributes end def show! get :show, id: app end def update_valid! put :update, id: app, app: app_attributes end def update_invalid! put :update, id: app, app: app_invalid_attributes end def delete! delete :destroy, id: app end def http_success expect(response).to be_success end def expect_template(view) expect(response).to render_template(view) end describe AppsController do render_views before(:each) do @developer = FactoryGirl.create(:developer) @developer.confirm! sign_in @developer end let(:app) { FactoryGirl.create(:app, developer: @developer) } let(:app_attributes) { FactoryGirl.attributes_for(:app) } let(:app_invalid_attributes) { FactoryGirl.attributes_for(:invalid_app) } describe 'GET #index' do it 'responds with an HTTP 200 status' do get :index http_success end it 'renders the :index view' do get :index expect_template(:index) end it 'populates @apps with the current_developers apps' do app = FactoryGirl.create(:app, :developer =&gt; @developer) get :index expect(assigns(:app)).to eq([app]) end end describe 'POST #create' do context 'with valid parameters' do it 'creates a new app' do expect { create_valid! }.to change(App, :count).by(1) end it 'redirects to the new app keys' do create_valid! expect(response).to redirect_to keys_app_path(App.last) end end context 'with invalid parameters' do it 'does not create the new app' do expect { create_invalid! }.to_not change(App, :count) end it 'renders the :new view' do create_invalid! expect_template(:new) end end end describe 'GET #show' do it 'responds with an HTTP 200 status' do show! http_success end it 'renders the :show view' do show! expect_template(:show) end it 'populates @app with the requested app' do show! expect(assigns(:app)).to eq(app) end end describe 'PUT #update' do context 'with valid parameters' do it 'locates the requested app' do update_valid! expect(assigns(:app)).to eq(app) end it 'changes app attributes' do update_valid! expect(app.name).to eq('Updated App') end it 'redirects to the updated app' do update_valid! expect(response).to redirect_to app end end context 'with invalid parameters' do it 'locates the requested app' do update_invalid! expect(assigns(:app)).to eq(app) end it 'does not change app attributes' do update_invalid! expect(app.name).to_not eq('Updated App') end it 'renders the :edit view' do update_invalid! expect_template(:edit) end end end describe 'DELETE #destroy' do it 'deletes the app' do expect { delete! }.to change(App, :count).by(-1) end it 'redirects to apps#index' do delete! expect(response).to redirect_to apps_url end end end </code></pre> <hr> <pre><code>count should have been changed by -1, but was changed by 0 - on DELETE #destroy expecting &lt;"new"&gt; but rendering with &lt;[]&gt; - on POST #create expected: "Updated App" got: "New App" - on PUT #update expecting &lt;"edit"&gt; but rendering with &lt;[]&gt; - on PUT #update expected: [#&lt;App id: nil, unique_id: "rOIc5p", developer_id: 18, name: "New App"&gt;] got: nil - on GET #index </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload