Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Rubocop Rspec and Resolve Issues Raised by Rubocop Rspec #51

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

toshitapandey
Copy link

Solves

#46

Purpose

  • Install Rubocop Rspec
  • Configure Rubocop Rspec
  • Resolve issues raised by Rubocop Rspec

@toshitapandey toshitapandey changed the title Integrate Rubocop Rspec and Resolve Issues Raised by Rubocop Rspec [WIP] Integrate Rubocop Rspec and Resolve Issues Raised by Rubocop Rspec Oct 8, 2022
@toshitapandey toshitapandey changed the title [WIP] Integrate Rubocop Rspec and Resolve Issues Raised by Rubocop Rspec Integrate Rubocop Rspec and Resolve Issues Raised by Rubocop Rspec Oct 8, 2022
@toshitapandey
Copy link
Author

@rodrigoapereira - There were 164 issues raised by rubocop-rspec. There are 54 pending issues, which I am not sure how to resolve. Please review.

@rodrigoapereira
Copy link

@toshitapandey Thank you! We appreciate your contribution, and I will review asap.

Copy link

@rodrigoapereira rodrigoapereira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some suggestions, could you fix? Also I will invite another maintainer to review your PR as well.

@@ -22,7 +22,7 @@

it_behaves_like 'with NOAUTH' do
it 'never checks the role' do
expect(current_user).not_to receive(:has_raw_role?)
allow(current_user).not_to receive(:has_raw_role?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd received an error when I ran rspec tests, because the use of not_to here. I'd recommend remove this allow, because if the method be called it will raise an exception, and made the test fail with:

#<InstanceDouble(Cassette::Authentication::User) (anonymous)> received unexpected message :has_raw_role? with (:something)

The spec output was:

Cassette::Authentication::Filter when controller belongs to Rails 3 behaves like controller behavior #validate_raw_role! behaves like with NOAUTH never checks the role
     Failure/Error: allow(current_user).not_to receive(:has_raw_role?)
       `allow(...).not_to receive` is not supported since it doesn't really make sense. What would it even mean?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum... I'm wondering here if this call doesn't make a network call underneath. Maybe we can just safely move to a expect(...).to have_received instead.

Leaving this test as is maybe lead us to false-positives when running rspec.
I wrote down a more complete suggestion in another part of the changes, @toshitapandey - can you see if it applies?

@@ -56,7 +56,7 @@

it_behaves_like 'with NOAUTH' do
it 'never checks the role' do
expect(current_user).not_to receive(:has_role?)
allow(current_user).not_to receive(:has_role?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same issue of previous comment, could you fix this?

# end
# end
end
it do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you merge these two its, naming as

it 'returns the configured username' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this won't be possible due to rubocop - it may be checking for "one expect per it" rule. But I'm favorable for adding a description for each it. What about

it "returns an user object` do
#...
end 

it "instantiates the returned user with the correct login information" do
#...
end

spec/cassette/authentication/cache_spec.rb Show resolved Hide resolved
@@ -34,20 +34,20 @@
it 'forwards to current_user' do
role = instance_double(String)

expect(current_user).to receive(:has_raw_role?).with(role).and_return(true)
allow(current_user).to receive(:has_raw_role?).with(role).and_return(true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the spec says the method is supposed to call has_raw_role, so expecting that would make sense. If it is rubocop that is suggesting this change, you can add an expect(current_user).to have_received(:has_raw_role?).with(role) after the exercise

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can not add expect(current_user).to have_received(:has_raw_role?).with(role) as it is throwing following error:
image

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The allow directive is used to create spies in the called method, allowing you to expect (or not) that it was called. I'm not sure about the rspec internal implementation of this method in specific, but, in summary, whenever you want to expect(object).to have received(:something), you must ensure that this object have already an spy on it listening to calls for #something method. In other words, you need a allow(object).to receive(:something) before the expectation.

@@ -8,63 +8,93 @@
describe '#initialize' do
context 'without a config' do
it 'forwards authorities parsing' do
expect(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', nil)
Cassette::Authentication::User.new(login: 'john.doe', name: 'John Doe', authorities: '[CUSTOMERAPI, SAPI]')
allow(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again it is expected to call the method, so a expect(...).to have_received is advised after the exercise

Cassette::Authentication::User.new(login: 'john.doe', name: 'John Doe',
authorities: '[CUSTOMERAPI, SAPI]', config: config)
described_class.new(login: 'john.doe', name: 'John Doe',
authorities: '[CUSTOMERAPI, SAPI]', config: config)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another method forwarding

@@ -74,23 +80,27 @@
end

describe '#validate_ticket' do
subject(:service) { Cassette.config.service }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You changed the subject here but don't actually use it

@@ -74,23 +80,27 @@
end

describe '#validate_ticket' do
subject(:service) { Cassette.config.service }

let(:ticket) { described_class.new }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need another instance of the described_class, the one in the start (with all the instance_double-ed dependencies is fine


describe Cassette::Client::Cache do
it 'uses the cache store set in configuration' do
# setup
global_cache = double('cache_store')
global_cache = instance_double(described_class, 'cache_store')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to be thorough, global cache is a ActiveSupport::Cache::Store and not a Cassette::Client::Cache

@@ -22,14 +18,13 @@
describe '.backend=' do
it 'sets the cache' do
# setup
global_cache = double('cache_store')
logger = Logger.new('/dev/null')
global_cache = instance_double(described_class, 'cache_store')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to be thorough, global cache is a ActiveSupport::Cache::Store and not a Cassette::Client::Cache

end
end
end

describe '.cache_backend' do
context 'when the cache_backend is already set' do
it 'returns the cache_backend set' do
new_cache = double('cache_backend')
new_cache = instance_double(described_class, 'cache_backend')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_backend is a ActiveSupport::Cache::Store

@@ -123,7 +117,7 @@ def keeping_logger(&block)
context 'when the cache_backend is not set' do
it 'returns Rails.cache if set' do
described_class.cache_backend = nil
rails_cache = double('cache')
rails_cache = instance_double(described_class, 'cache')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_backend is a ActiveSupport::Cache::Store

@@ -8,63 +8,93 @@
describe '#initialize' do
context 'without a config' do
it 'forwards authorities parsing' do
expect(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', nil)
Cassette::Authentication::User.new(login: 'john.doe', name: 'John Doe', authorities: '[CUSTOMERAPI, SAPI]')
allow(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change from expect to allow, is this spec still testing the original behavior? What do you think about using expect(...).to have_received along with this allow, instead? This way you still ensure the original expectancy when the test used expect(...).to receive, but without the rubocop warning :)

@@ -22,7 +22,7 @@

it_behaves_like 'with NOAUTH' do
it 'never checks the role' do
expect(current_user).not_to receive(:has_raw_role?)
allow(current_user).not_to receive(:has_raw_role?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum... I'm wondering here if this call doesn't make a network call underneath. Maybe we can just safely move to a expect(...).to have_received instead.

Leaving this test as is maybe lead us to false-positives when running rspec.
I wrote down a more complete suggestion in another part of the changes, @toshitapandey - can you see if it applies?

@@ -38,21 +40,25 @@
end

context 'when not cached' do
def auth
subject
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand the need for this function here... wouldn't a simple call to authentication at line 55 match the conditions required by rubocop?

# end
# end
end
it do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this won't be possible due to rubocop - it may be checking for "one expect per it" rule. But I'm favorable for adding a description for each it. What about

it "returns an user object` do
#...
end 

it "instantiates the returned user with the correct login information" do
#...
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants