-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Conversation
@rodrigoapereira - There were 164 issues raised by |
@toshitapandey Thank you! We appreciate your contribution, and I will review asap. |
There was a problem hiding this 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?) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 it
s, naming as
it 'returns the configured username' do
There was a problem hiding this comment.
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
@@ -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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 } |
There was a problem hiding this comment.
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 } |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
Solves
#46
Purpose