Enable RSpec/BeforeAfterAll rule and fix all issues

This cop tries to avoid state leaking between examples. The fixes done on the code don't follow the suggested path, since the usage of `before(:all)` was not really useful. By using RSpec's `let` method we achieve same goals but with much better and readable tests.

Check Cop description at http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeforeAfterAll
This commit is contained in:
Bertocq
2018-01-06 20:18:24 +01:00
parent 6555d20a6a
commit de2816a6a5
4 changed files with 32 additions and 39 deletions

View File

@@ -24,3 +24,6 @@ Metrics/LineLength:
Layout/IndentationConsistency: Layout/IndentationConsistency:
EnforcedStyle: rails EnforcedStyle: rails
RSpec/BeforeAfterAll:
Enabled: true

View File

@@ -1,66 +1,60 @@
require 'rails_helper' require 'rails_helper'
describe ManagerAuthenticator do describe ManagerAuthenticator do
let(:authenticator) { ManagerAuthenticator.new(login: "JJB033", clave_usuario: "31415926", fecha_conexion: "20151031135905") }
describe 'initialization params' do describe 'initialization params' do
it 'should cause auth to return false if blank login' do it 'should cause auth to return false if blank login' do
authenticator = ManagerAuthenticator.new(login: "", clave_usuario: "31415926", fecha_conexion: "20151031135905") blank_login_authenticator = ManagerAuthenticator.new(login: "", clave_usuario: "31415926", fecha_conexion: "20151031135905")
expect(authenticator.auth).to be false expect(blank_login_authenticator.auth).to be false
end end
it 'should cause auth to return false if blank user_key' do it 'should cause auth to return false if blank user_key' do
authenticator = ManagerAuthenticator.new(login: "JJB033", clave_usuario: "", fecha_conexion: "20151031135905") blank_user_key_authenticator = ManagerAuthenticator.new(login: "JJB033", clave_usuario: "", fecha_conexion: "20151031135905")
expect(authenticator.auth).to be false expect(blank_user_key_authenticator.auth).to be false
end end
it 'should cause auth to return false if blank date' do it 'should cause auth to return false if blank date' do
authenticator = ManagerAuthenticator.new(login: "JJB033", clave_usuario: "31415926", fecha_conexion: "") blank_date_authenticator = ManagerAuthenticator.new(login: "JJB033", clave_usuario: "31415926", fecha_conexion: "")
expect(authenticator.auth).to be false expect(blank_date_authenticator.auth).to be false
end end
end end
describe '#auth' do describe '#auth' do
before(:all) do
@authenticator = ManagerAuthenticator.new(login: "JJB033", clave_usuario: "31415926", fecha_conexion: "20151031135905")
end
it 'should return false if not manager_exists' do it 'should return false if not manager_exists' do
allow(@authenticator).to receive(:manager_exists?).and_return(false) allow(authenticator).to receive(:manager_exists?).and_return(false)
allow(@authenticator).to receive(:application_authorized?).and_return(true) allow(authenticator).to receive(:application_authorized?).and_return(true)
expect(@authenticator.auth).to be false expect(authenticator.auth).to be false
end end
it 'should return false if not application_authorized' do it 'should return false if not application_authorized' do
allow(@authenticator).to receive(:manager_exists?).and_return(true) allow(authenticator).to receive(:manager_exists?).and_return(true)
allow(@authenticator).to receive(:application_authorized?).and_return(false) allow(authenticator).to receive(:application_authorized?).and_return(false)
expect(@authenticator.auth).to be false expect(authenticator.auth).to be false
end end
it 'should return ok if manager_exists and application_authorized' do it 'should return ok if manager_exists and application_authorized' do
allow(@authenticator).to receive(:manager_exists?).and_return(true) allow(authenticator).to receive(:manager_exists?).and_return(true)
allow(@authenticator).to receive(:application_authorized?).and_return(true) allow(authenticator).to receive(:application_authorized?).and_return(true)
expect(@authenticator.auth).to be_truthy expect(authenticator.auth).to be_truthy
end end
end end
describe 'SOAP' do describe 'SOAP' do
before(:all) do
@authenticator = ManagerAuthenticator.new(login: "JJB033", clave_usuario: "31415926", fecha_conexion: "20151031135905")
end
it 'should call the verification user method' do it 'should call the verification user method' do
message = { ub: {user_key: "31415926", date: "20151031135905"} } message = { ub: {user_key: "31415926", date: "20151031135905"} }
allow(@authenticator).to receive(:application_authorized?).and_return(true) allow(authenticator).to receive(:application_authorized?).and_return(true)
expect(@authenticator.send(:client)).to receive(:call).with(:get_status_user_data, message: message) expect(authenticator.send(:client)).to receive(:call).with(:get_status_user_data, message: message)
@authenticator.auth authenticator.auth
end end
it 'should call the permissions check method' do it 'should call the permissions check method' do
allow(@authenticator).to receive(:manager_exists?).and_return(true) allow(authenticator).to receive(:manager_exists?).and_return(true)
expect(@authenticator.send(:client)).to receive(:call).with(:get_applications_user_list, message: { ub: {user_key: "31415926"} }) expect(authenticator.send(:client)).to receive(:call).with(:get_applications_user_list, message: { ub: {user_key: "31415926"} })
@authenticator.auth authenticator.auth
end end
end end
end end

View File

@@ -15,10 +15,6 @@ describe 'Settings Rake' do
Rake.application.invoke_task 'settings:per_page_code_migration' Rake.application.invoke_task 'settings:per_page_code_migration'
end end
after(:all) do
Setting['per_page_code_head'] = ''
end
context 'Neither per_page_code_head or per_page_code Settings exist' do context 'Neither per_page_code_head or per_page_code Settings exist' do
before do before do
Setting.where(key: 'per_page_code').first&.destroy Setting.where(key: 'per_page_code').first&.destroy

View File

@@ -45,7 +45,7 @@ describe Organization do
end end
describe "self.search" do describe "self.search" do
before(:all) {@organization = create(:organization, name: "Watershed", user: create(:user, phone_number: "333"))} let!(:organization) { create(:organization, name: "Watershed", user: create(:user, phone_number: "333")) }
it "returns no results if search term is empty" do it "returns no results if search term is empty" do
expect(Organization.search(" ").size).to eq(0) expect(Organization.search(" ").size).to eq(0)
@@ -55,19 +55,19 @@ describe Organization do
expect(Organization.search("Greenpeace").size).to eq 0 expect(Organization.search("Greenpeace").size).to eq 0
search = Organization.search("Tershe") search = Organization.search("Tershe")
expect(search.size).to eq 1 expect(search.size).to eq 1
expect(search.first).to eq @organization expect(search.first).to eq organization
end end
scenario "finds by users email" do scenario "finds by users email" do
search = Organization.search(@organization.user.email) search = Organization.search(organization.user.email)
expect(search.size).to eq 1 expect(search.size).to eq 1
expect(search.first).to eq @organization expect(search.first).to eq organization
end end
scenario "finds by users phone number" do scenario "finds by users phone number" do
search = Organization.search(@organization.user.phone_number) search = Organization.search(organization.user.phone_number)
expect(search.size).to eq 1 expect(search.size).to eq 1
expect(search.first).to eq @organization expect(search.first).to eq organization
end end
end end
end end