lib/templates/rspec/scaffold/controller_spec.rb in stationed-0.4.0 vs lib/templates/rspec/scaffold/controller_spec.rb in stationed-0.5.0

- old
+ new

@@ -1,19 +1,18 @@ -require 'spec_helper' - <% module_namespacing do -%> -describe <%= controller_class_name %>Controller do +RSpec.describe <%= controller_class_name %>Controller, type: :controller do subject { response } <% unless options[:singleton] -%> describe 'GET index' do let(:<%= table_name %>) { build_stubbed_list :<%= file_name %>, 2 } before do allow(<%= class_name %>).to receive(:all).and_return(<%= table_name %>) - <%= class_name %>Policy.any_instance.stub(:index?).and_return(true) + allow_any_instance_of(<%= class_name %>Policy).to receive(:index?).and_return(true) allow(Array).to receive(:policy_class).and_return(<%= class_name %>Policy) + allow(<%= table_name %>).to receive(:page).and_return(<%= table_name %>) allow(<%= table_name %>).to receive(:decorate).and_return(<%= table_name %>) end describe 'response' do before { get :index } @@ -36,11 +35,11 @@ describe 'GET show' do let(:<%= file_name %>) { build_stubbed :<%= file_name %> } before do allow(<%= class_name %>).to receive(:find).with(<%= file_name %>.to_param).and_return(<%= file_name %>) - <%= class_name %>Policy.any_instance.stub(:show?).and_return(true) + allow_any_instance_of(<%= class_name %>Policy).to receive(:show?).and_return(true) end describe 'response' do before { get :show, id: <%= file_name %>.to_param } it { should render_template('show') } @@ -61,11 +60,11 @@ describe 'GET new' do let(:<%= file_name %>) { build :<%= file_name %> } before do allow(<%= class_name %>).to receive(:new).and_return(<%= file_name %>) - <%= class_name %>Policy.any_instance.stub(:new?).and_return(true) + allow_any_instance_of(<%= class_name %>Policy).to receive(:new?).and_return(true) end describe 'response' do before { get :new } it { should render_template('new') } @@ -86,11 +85,11 @@ describe 'GET edit' do let(:<%= file_name %>) { build_stubbed :<%= file_name %> } before do allow(<%= class_name %>).to receive(:find).with(<%= file_name %>.to_param).and_return(<%= file_name %>) - <%= class_name %>Policy.any_instance.stub(:edit?).and_return(true) + allow_any_instance_of(<%= class_name %>Policy).to receive(:edit?).and_return(true) end describe 'response' do before { get :edit, id: <%= file_name %>.to_param } it { should render_template('edit') } @@ -108,11 +107,11 @@ end end describe 'POST create' do before do - <%= class_name %>Policy.any_instance.stub(:create?).and_return(true) + allow_any_instance_of(<%= class_name %>Policy).to receive(:create?).and_return(true) end describe 'with valid params' do it 'creates a new <%= class_name %>' do expect { @@ -156,30 +155,32 @@ describe 'PUT update' do let(:<%= file_name %>) { build_stubbed :<%= file_name %> } before do allow(<%= class_name %>).to receive(:find).with(<%= file_name %>.to_param).and_return(<%= file_name %>) - <%= class_name %>Policy.any_instance.stub(:update?).and_return(true) + allow_any_instance_of(<%= class_name %>Policy).to receive(:update?).and_return(true) end describe 'with valid params' do + let(:attributes) { attributes_for(:<%= file_name %>) } + before do allow(<%= file_name %>).to receive(:update).and_return(true) end it 'updates the requested <%= ns_file_name %>' do - expect(<%= file_name %>).to receive(:update).with(attributes_for(:<%= file_name %>).stringify_keys) - put :update, id: <%= file_name %>.to_param, <%= ns_file_name %>: attributes_for(:<%= file_name %>) + expect(<%= file_name %>).to receive(:update).with(attributes.stringify_keys) + put :update, id: <%= file_name %>.to_param, <%= ns_file_name %>: attributes end it 'assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>' do - put :update, id: <%= file_name %>.to_param, <%= ns_file_name %>: attributes_for(:<%= file_name %>) + put :update, id: <%= file_name %>.to_param, <%= ns_file_name %>: attributes expect(assigns(:<%= ns_file_name %>)).to eq(<%= file_name %>) end it 'redirects to the <%= ns_file_name %>' do - put :update, id: <%= file_name %>.to_param, <%= ns_file_name %>: attributes_for(:<%= file_name %>) + put :update, id: <%= file_name %>.to_param, <%= ns_file_name %>: attributes expect(response).to redirect_to(<%= file_name %>) end end describe 'with invalid params' do @@ -205,10 +206,10 @@ before do allow(<%= class_name %>).to receive(:find).with(<%= file_name %>.to_param).and_return(<%= file_name %>) allow(<%= file_name %>).to receive(:destroy).and_return(true) allow(<%= file_name %>).to receive(:persisted?).and_return(false) - <%= class_name %>Policy.any_instance.stub(:destroy?).and_return(true) + allow(<%= class_name %>Policy).to receive(:destroy?).and_return(true) end it 'destroys the requested <%= ns_file_name %>' do expect(<%= file_name %>).to receive(:destroy) delete :destroy, id: <%= file_name %>.to_param