diff --git a/Gemfile b/Gemfile index b420e03..3918586 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,7 @@ gem 'rails', '>= 5.0.0.beta1' gem 'rake' gem 'sdoc' -gem 'rspec-rails' +gem 'rspec-rails', '>= 3.5.0.beta2' gem 'haml' diff --git a/Rakefile b/Rakefile index 8bdf464..3a75943 100644 --- a/Rakefile +++ b/Rakefile @@ -65,8 +65,8 @@ namespace :test do "require 'simplecov'\nSimpleCov.start do\n" + " coverage_dir 'coverage/spec'\nend\n") file_replace(File.join(TEST_APP_ROOT, 'spec', 'rails_helper.rb'), - '# Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }', - 'Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }') + "# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }", + "Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }") end end diff --git a/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb b/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb index 80e0877..8a8af6e 100644 --- a/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +++ b/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb @@ -68,7 +68,6 @@ end it 'provides entries helper method' do - is_expected.to render_template('index') expect(entries).to be(controller.send(:entries)) end end @@ -175,7 +174,7 @@ session[:list_params] = {} session[:list_params]['/crud_test_models'] = { 'q' => 'DDD', 'sort' => 'chatty', 'sort_dir' => 'desc' } - get :index, returning: true + get :index, params: { returning: true } end it_is_expected_to_respond @@ -194,7 +193,6 @@ context '.js', format: :js, combine: 'ijs' do it_is_expected_to_respond - it_is_expected_to_assign_entries it { expect(response.body).to eq('index js') } end end @@ -202,7 +200,7 @@ describe_action :get, :new do context 'plain', combine: 'new' do it 'assigns companions' do - expect(assigns(:companions)).to be_present + expect(ivar(:companions)).to be_present end it 'calls two render callbacks' do @@ -221,7 +219,7 @@ it { is_expected.to redirect_to(crud_test_models_path) } it 'does not set companions' do - expect(assigns(:companions)).to be_nil + expect(ivar(:companions)).to be_nil end end end @@ -244,7 +242,6 @@ context 'plain', combine: 'chcp' do it_is_expected_to_respond - it_is_expected_to_render('new') it_is_expected_to_persist_entry(false) it_is_expected_to_have_flash(:alert) @@ -253,7 +250,7 @@ end it 'assigns companions' do - expect(assigns(:companions)).to be_present + expect(ivar(:companions)).to be_present end it 'calls the correct callbacks' do @@ -291,13 +288,12 @@ context 'plain', combine: 'chip' do it_is_expected_to_respond - it_is_expected_to_render('new') it_is_expected_to_persist_entry(false) it_is_expected_to_not_have_flash(:notice) it_is_expected_to_not_have_flash(:alert) it 'assigns companions' do - expect(assigns(:companions)).to be_present + expect(ivar(:companions)).to be_present end it 'calls the correct callbacks' do @@ -321,7 +317,7 @@ it_is_expected_to_render_json it 'does not assign companions' do - expect(assigns(:companions)).to be_nil + expect(ivar(:companions)).to be_nil end it 'calls the correct callbacks' do @@ -354,7 +350,6 @@ context '.html', combine: 'uhivp' do it_is_expected_to_respond - it_is_expected_to_render('edit') it_is_expected_to_not_have_flash(:notice) it 'changes entry' do diff --git a/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb b/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb index 4b9df01..7e40b92 100644 --- a/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +++ b/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb @@ -27,8 +27,9 @@ let(:model_identifier) { controller.model_identifier } let(:test_params) { scope_params } let(:sort_column) { model_class.column_names.first } - let(:entry) { assigns(controller.send(:ivar_name, model_class)) } - let(:entries) { assigns(controller.send(:ivar_name, model_class).pluralize) } + let(:ivar_name) { controller.send(:ivar_name, model_class) } + let(:entry) { ivar(ivar_name) } + let(:entries) { ivar(ivar_name.pluralize) } let(:search_value) do field = controller.search_columns.first val = test_entry[field].to_s @@ -53,8 +54,6 @@ unless: skip?(options, %w(index html plain)), combine: 'ihp' do it_is_expected_to_respond - it_is_expected_to_assign_entries - it_is_expected_to_render end context 'search', @@ -106,7 +105,6 @@ unless: skip?(options, %w(index json)), combine: 'ij' do it_is_expected_to_respond - it_is_expected_to_assign_entries it { expect(response.body).to start_with('[{') } end end @@ -123,8 +121,6 @@ unless: skip?(options, %w(show html plain)), combine: 'sh' do it_is_expected_to_respond - it_is_expected_to_assign_entry - it_is_expected_to_render end context 'with non-existing id', @@ -144,7 +140,6 @@ unless: skip?(options, %w(show json)), combine: 'sj' do it_is_expected_to_respond - it_is_expected_to_assign_entry it_is_expected_to_render_json end end @@ -155,7 +150,6 @@ unless: skip?(options, %w(new plain)), combine: 'new' do it_is_expected_to_respond - it_is_expected_to_render it_is_expected_to_persist_entry(false) end @@ -191,7 +185,6 @@ failing: true, unless: skip?(options, %w(create html invalid)), combine: 'chi' do - it_is_expected_to_render('new') it_is_expected_to_persist_entry(false) it_is_expected_to_set_attrs(:new) it_is_expected_to_not_have_flash(:notice) @@ -228,8 +221,6 @@ unless: skip?(options, %w(edit)), combine: 'edit' do it_is_expected_to_respond - it_is_expected_to_render - it_is_expected_to_assign_entry end describe_action :put, :update, @@ -257,7 +248,6 @@ failing: true, unless: skip?(options, %w(update html invalid)), combine: 'uhi' do - it_is_expected_to_render('edit') it_is_expected_to_set_attrs(:edit) it_is_expected_to_not_have_flash(:notice) end diff --git a/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb b/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb index 3531bb6..631c8ef 100644 --- a/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +++ b/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb @@ -9,13 +9,14 @@ module CrudControllerTestHelper def perform_request m = RSpec.current_example.metadata example_params = respond_to?(:params) ? send(:params) : {} - params = scope_params.merge(format: m[:format]) + params = scope_params.dup + params.merge!(format: m[:format]) if m[:format] params.merge!(id: test_entry.id) if m[:id] params.merge!(example_params) if m[:method] == :get && m[:format] == :js - xhr(:get, m[:action], params) + get m[:action], params: params, xhr: true else - send(m[:method], m[:action], params) + send(m[:method], m[:action], params: params) end end @@ -56,6 +57,10 @@ def restore_request @request = @@current_request end + def ivar(name) + controller.instance_variable_get("@#{name}") + end + # The params defining the nesting of the test entry. def scope_params params = {} @@ -103,29 +108,6 @@ def it_is_expected_to_respond(status = 200) it { expect(response.status).to eq(status) } end - # Test that entries are assigned. - def it_is_expected_to_assign_entries - it 'assigns entries' do - expect(entries).to be_present - end - end - - # Test that entry is assigned. - def it_is_expected_to_assign_entry - it 'assigns entry' do - expect(entry).to eq(test_entry) - end - end - - # Test that the given template or the main template of the action under - # test is rendered. - def it_is_expected_to_render(template = nil) - it do - template ||= RSpec.current_example.metadata[:action] - is_expected.to render_template(template) - end - end - # Test that a json response is rendered. def it_is_expected_to_render_json it { expect(response.body).to start_with('{') } @@ -147,6 +129,7 @@ def it_is_expected_to_set_attrs(action = nil) def it_is_expected_to_redirect_to_index it do is_expected.to redirect_to scope_params.merge(action: 'index', + id: nil, returning: true) end end diff --git a/test/templates/spec/controllers/admin/cities_controller_spec.rb b/test/templates/spec/controllers/admin/cities_controller_spec.rb index f734f5a..f808028 100644 --- a/test/templates/spec/controllers/admin/cities_controller_spec.rb +++ b/test/templates/spec/controllers/admin/cities_controller_spec.rb @@ -31,7 +31,7 @@ end it 'sets parent variable' do - expect(assigns(:country)).to eq(test_entry.country) + expect(ivar(:country)).to eq(test_entry.country) end it 'uses correct model_scope' do @@ -50,7 +50,7 @@ end it 'sets parent variable' do - expect(assigns(:country)).to eq(test_entry.country) + expect(ivar(:country)).to eq(test_entry.country) end end diff --git a/test/templates/spec/controllers/people_controller_spec.rb b/test/templates/spec/controllers/people_controller_spec.rb index 062b0a7..f06063a 100644 --- a/test/templates/spec/controllers/people_controller_spec.rb +++ b/test/templates/spec/controllers/people_controller_spec.rb @@ -54,7 +54,6 @@ describe_action :get, :show, id: true do context '.js', format: :js do it_is_expected_to_respond - it_is_expected_to_render it { expect(response.body).to match(/\$\('#content'\)/) } end end @@ -62,7 +61,6 @@ describe_action :get, :edit, id: true do context '.js', format: :js do it_is_expected_to_respond - it_is_expected_to_render it { expect(response.body).to match(/\$\('#content'\)/) } end end @@ -73,7 +71,6 @@ let(:params) { { person: { name: 'New Name' } } } it_is_expected_to_respond - it_is_expected_to_render it { expect(response.body).to match(/\$\('#content'\)/) } end @@ -81,7 +78,6 @@ let(:params) { { person: { name: ' ' } } } it_is_expected_to_respond - it_is_expected_to_render it { expect(response.body).to match(/alert/) } end end