diff --git a/.rubocop.yml b/.rubocop.yml index 84b73c5..d8ca2ab 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,7 @@ inherit_from: .rubocop_todo.yml +require: rubocop-rspec + AllCops: Exclude: - 'spec/dummy/db/**/*.rb' @@ -19,3 +21,11 @@ Metrics/AbcSize: Metrics/MethodLength: Exclude: - 'app/controllers/logux_controller.rb' + +RSpec/DescribeClass: + Exclude: + - 'spec/logux/tasks/*.rb' + - 'spec/requests/*.rb' + +RSpec/ExpectChange: + EnforcedStyle: block diff --git a/Gemfile.lock b/Gemfile.lock index fd3ee0b..5ae2be5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -180,6 +180,8 @@ GEM rainbow (>= 2.2.2, < 4.0) ruby-progressbar (~> 1.7) unicode-display_width (~> 1.0, >= 1.0.1) + rubocop-rspec (1.27.0) + rubocop (>= 0.56.0) ruby-progressbar (1.9.0) safe_yaml (1.0.4) simplecov (0.16.1) @@ -229,6 +231,7 @@ DEPENDENCIES rspec-live_controllers rspec-rails rubocop + rubocop-rspec simplecov sqlite3 timecop diff --git a/logux_rails.gemspec b/logux_rails.gemspec index c623bb4..067d16d 100644 --- a/logux_rails.gemspec +++ b/logux_rails.gemspec @@ -37,6 +37,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rspec-live_controllers' spec.add_development_dependency 'rspec-rails' spec.add_development_dependency 'rubocop' + spec.add_development_dependency 'rubocop-rspec' spec.add_development_dependency 'simplecov' spec.add_development_dependency 'sqlite3' spec.add_development_dependency 'timecop' diff --git a/spec/logux/action_caller_spec.rb b/spec/logux/action_caller_spec.rb index f361f35..7ddabb4 100644 --- a/spec/logux/action_caller_spec.rb +++ b/spec/logux/action_caller_spec.rb @@ -8,10 +8,8 @@ let(:meta) { create(:logux_meta) } describe '#call!' do - subject { action_caller.call! } - it 'raise error' do - expect { subject }.to raise_error(Logux::NoActionError) + expect { action_caller.call! }.to raise_error(Logux::NoActionError) end context 'when action defined' do @@ -25,6 +23,8 @@ def add end end + let(:result) { action_caller.call! } + after do Actions::User.send :undef_method, :add Actions.send :remove_const, :User @@ -32,7 +32,7 @@ def add end it 'return ok' do - expect(subject.status).to eq(:ok) + expect(result.status).to eq(:ok) end end end diff --git a/spec/logux/action_controller_spec.rb b/spec/logux/action_controller_spec.rb index 2dd20cf..256b079 100644 --- a/spec/logux/action_controller_spec.rb +++ b/spec/logux/action_controller_spec.rb @@ -9,18 +9,21 @@ let(:meta) { Logux::Meta.new } describe '#respond' do - subject { action_controller.respond(:processed) } + let(:response) { action_controller.respond(:processed) } it 'returns logux response' do - expect(subject.status).to eq(:processed) - expect(subject.action).to eq(action) - expect(subject.meta).to have_key(:time) - expect(subject.custom_data).to be_nil + expect(response).to have_attributes( + status: :processed, action: action, custom_data: nil + ) + end + + it 'sets the meta with time' do + expect(response.meta).to have_key(:time) end end describe '.verify_authorized!' do - subject { described_class.verify_authorized! } + let(:verify_authorized!) { described_class.verify_authorized! } around do |example| Logux.configuration.verify_authorized = false @@ -29,7 +32,7 @@ end it 'sets to true' do - expect { subject } + expect { verify_authorized! } .to change { Logux.configuration.verify_authorized } .from(false) .to(true) @@ -37,12 +40,12 @@ end describe '.unverify_authorized!' do - subject { described_class.unverify_authorized! } + let(:unverify_authorized!) { described_class.unverify_authorized! } before { Logux.configuration.verify_authorized = true } it 'sets to false' do - expect { subject } + expect { unverify_authorized! } .to change { Logux.configuration.verify_authorized } .from(true) .to(false) diff --git a/spec/logux/actions_spec.rb b/spec/logux/actions_spec.rb index 6a92a71..ba9f30c 100644 --- a/spec/logux/actions_spec.rb +++ b/spec/logux/actions_spec.rb @@ -8,24 +8,24 @@ describe '#action_type' do subject { actions.action_type } - it { should eq 'add' } + it { is_expected.to eq 'add' } end describe '#action_name' do subject { actions.action_name } - it { should eq 'user' } + it { is_expected.to eq 'user' } end describe '#channel_name' do subject { actions.channel_name } - it { should eq 'project' } + it { is_expected.to eq 'project' } end describe '#channel_id' do subject { actions.channel_id } - it { should eq '123' } + it { is_expected.to eq '123' } end end diff --git a/spec/logux/add_spec.rb b/spec/logux/add_spec.rb index ac2fdf9..f45064b 100644 --- a/spec/logux/add_spec.rb +++ b/spec/logux/add_spec.rb @@ -6,8 +6,6 @@ let(:request) { described_class.new } describe '#call' do - subject { request.call(data, meta: meta) } - let(:data) { [{ id: 1 }, { id: 2 }] } let(:meta) { create(:logux_meta) } let(:logux_request) do @@ -19,7 +17,7 @@ end it 'return processed' do - expect { subject }.to send_to_logux(logux_request) + expect { request.call(data, meta: meta) }.to send_to_logux(logux_request) end end end diff --git a/spec/logux/channel_controller_spec.rb b/spec/logux/channel_controller_spec.rb index 19dd7cc..65ea39f 100644 --- a/spec/logux/channel_controller_spec.rb +++ b/spec/logux/channel_controller_spec.rb @@ -9,11 +9,11 @@ let(:meta) { Logux::Meta.new } describe '#subscribe' do - subject { channel_controller.subscribe } + let(:subscribe) { channel_controller.subscribe } context 'when ActiveRecord defined' do it 'tries to find record by chanel data' do - expect { subject }.to send_to_logux + expect { subscribe }.to send_to_logux end end end diff --git a/spec/logux/class_finder_spec.rb b/spec/logux/class_finder_spec.rb index 7a93dee..401964a 100644 --- a/spec/logux/class_finder_spec.rb +++ b/spec/logux/class_finder_spec.rb @@ -6,22 +6,22 @@ let(:finder) { described_class.new(params) } describe '#class_name' do - subject { finder.class_name } + let(:class_name) { finder.class_name } let(:params) { create(:logux_actions_add, type: 'test/test/name/try/user/add') } it 'returns nested classes' do - expect(subject).to eq('Test::Test::Name::Try::User') + expect(class_name).to eq('Test::Test::Name::Try::User') end end describe '#find_policy_class' do - subject { finder.find_policy_class } + let(:policy_class) { finder.find_policy_class } let(:params) { create(:logux_actions_add, type: 'test/test/name/try/user/add') } it 'raise an error' do - expect { subject }.to raise_error(Logux::NoPolicyError) + expect { policy_class }.to raise_error(Logux::NoPolicyError) end end end diff --git a/spec/logux/client_spec.rb b/spec/logux/client_spec.rb index 7528381..f54b688 100644 --- a/spec/logux/client_spec.rb +++ b/spec/logux/client_spec.rb @@ -7,10 +7,8 @@ let(:params) { create(:logux_actions_add) } describe '#post' do - subject { client.post(params) } - it 'performs request' do - expect { subject }.to send_to_logux(params) + expect { client.post(params) }.to send_to_logux(params) end end end diff --git a/spec/logux/node_spec.rb b/spec/logux/node_spec.rb index c779c57..81537b9 100644 --- a/spec/logux/node_spec.rb +++ b/spec/logux/node_spec.rb @@ -6,10 +6,10 @@ let(:node) { described_class.instance } describe '#generate_action_id' do - subject { node.generate_action_id } + let(:action_id) { node.generate_action_id } it 'returns correct id' do - expect(subject).to match(/^[0-9]{13} server:.{8} 0$/) + expect(action_id).to match(/^[0-9]{13} server:.{8} 0$/) end context 'with action at the same time', timecop: true do @@ -20,20 +20,20 @@ end it 'returns 1 in sequence' do - expect(subject).to match(/^[0-9]{13} server:.{8} 1$/) + expect(action_id).to match(/^[0-9]{13} server:.{8} 1$/) end end end describe '#node_id' do - subject { node.node_id } + let(:node_id) { node.node_id } it 'generates nanoid' do - expect(subject).not_to be_empty + expect(node_id).not_to be_empty end it "doesn't change from call to call" do - expect(subject).to eq(node.node_id) + expect(node_id).to eq(node.node_id) end end end diff --git a/spec/logux/policy_caller_spec.rb b/spec/logux/policy_caller_spec.rb index aa6d4e3..6d1da1f 100644 --- a/spec/logux/policy_caller_spec.rb +++ b/spec/logux/policy_caller_spec.rb @@ -6,14 +6,20 @@ let(:policy_caller) { described_class.new(action: action, meta: meta) } describe '.call!' do - subject { policy_caller.call! } + let(:call!) { policy_caller.call! } let(:action) { Logux::Actions.new(type: 'test/test') } let(:meta) { {} } - it 'doesn\'t raise an error' do - expect(Logux.logger).to receive(:warn).once - subject + context 'when request is not verified' do + before do + allow(Logux.logger).to receive(:warn) + call! + end + + it 'doesn\'t raise an error' do + expect(Logux.logger).to have_received(:warn).once + end end context 'when verify_authorized' do @@ -24,7 +30,7 @@ end it 'raises an error' do - expect { subject }.to raise_error(Logux::NoPolicyError) + expect { call! }.to raise_error(Logux::NoPolicyError) end end end diff --git a/spec/logux_spec.rb b/spec/logux_spec.rb index 258c71c..194a404 100644 --- a/spec/logux_spec.rb +++ b/spec/logux_spec.rb @@ -6,21 +6,20 @@ end describe '.add' do - subject { described_class.add(type) } + before { described_class.add(type) } let(:type) { [] } it 'makes request' do - subject expect(WebMock).to have_requested(:post, Logux.configuration.logux_host) end end describe '.generate_action_id' do - subject { described_class.generate_action_id } + let(:action_id) { described_class.generate_action_id } it 'returns correct action id' do - expect(subject).not_to be_empty + expect(action_id).not_to be_empty end end end diff --git a/spec/requests/request_from_logux_server_spec.rb b/spec/requests/request_from_logux_server_spec.rb index 57d60e7..0881413 100644 --- a/spec/requests/request_from_logux_server_spec.rb +++ b/spec/requests/request_from_logux_server_spec.rb @@ -3,11 +3,12 @@ require 'rails_helper' describe 'Logux response' do - subject do + let(:request_logux) do post('/logux', params: logux_params, as: :json) end + let(:password) { Logux.configuration.password } let(:logux_params) do @@ -23,21 +24,22 @@ ] } end let(:params) { Logux.configuration.password } - let(:logux_response) do - [ - ['processed', '219_856_768 clientid 0'], - ['processed', '219_856_768 clientid 0'] - ] - end - it 'does return correct body' do - subject - expect(response.stream).to have_chunk(['approved', '219_856_768 clientid 0']) - expect(response.stream).to have_chunk(logux_response[0]) - expect(response.stream).to have_chunk(logux_response[1]) + context 'when authorized' do + before { request_logux } + + it 'returns approved chunk' do + expect(response.stream).to have_chunk(['approved', '219_856_768 clientid 0']) + end + + it 'returns processed chunk' do + expect(response.stream).to have_chunk(['processed', '219_856_768 clientid 0']) + end end context 'when no authorized' do + before { request_logux } + let(:logux_params) do { version: 0, password: password, @@ -52,16 +54,16 @@ end it 'does return correct body' do - subject expect(response.stream).to have_chunk(logux_response) end end context 'when password wrong' do + before { request_logux } + let(:password) { '12345' } it 'does return error' do - subject expect(response.stream).to start_from_chunk([:error]) end end @@ -82,7 +84,7 @@ end it 'returns correct chunk' do - expect { subject }.to change { logux_store.size }.by(1) + expect { request_logux }.to change { logux_store.size }.by(1) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fdfc86f..91639be 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,7 +8,10 @@ Coveralls::SimpleCov::Formatter ]) Coveralls.wear! -SimpleCov.start +SimpleCov.start do + add_filter '/spec/' + add_filter '/lib/logux/test/' +end require 'bundler/setup' require 'factory_bot'