Skip to content

Commit

Permalink
fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed Mar 14, 2016
1 parent 4110372 commit bfcdd73
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -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'

Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -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

Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -194,15 +193,14 @@

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

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
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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

Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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('{') }
Expand All @@ -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
Expand Down
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 0 additions & 4 deletions test/templates/spec/controllers/people_controller_spec.rb
Expand Up @@ -54,15 +54,13 @@
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

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
Expand All @@ -73,15 +71,13 @@
let(:params) { { person: { name: 'New Name' } } }

it_is_expected_to_respond
it_is_expected_to_render
it { expect(response.body).to match(/\$\('#content'\)/) }
end

context 'with invalid params' do
let(:params) { { person: { name: ' ' } } }

it_is_expected_to_respond
it_is_expected_to_render
it { expect(response.body).to match(/alert/) }
end
end
Expand Down

0 comments on commit bfcdd73

Please sign in to comment.