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

Suggestion: Check for constants being defined in specs #197

Closed
bquorning opened this issue Sep 1, 2016 · 5 comments

Comments

@bquorning
Copy link
Member

commented Sep 1, 2016

When you define a constant (modules, classes, constants) in a spec, it’s defined in global scope. That may lead to confusing spec errors:

context "one" do
  class Foo
    def self.bar
      1
    end
  end

  it "should be 1" do
    expect(Foo.bar).to eq(1)
  end
end

context "forty two" do
  class Foo
    def self.bar
      42
    end
  end

  it "should be 42" do
    expect(Foo.bar).to eq(42)
  end
end

With the output

» rspec class_scope_spec.rb
F.

Failures:

  1) one should be 1
     Failure/Error: expect(Foo.bar).to eq(1)

       expected: 1
            got: 42

       (compared using ==)
     # ./class_scope_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.01052 seconds (files took 0.11059 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./class_scope_spec.rb:8 # one should be 1
@backus

This comment has been minimized.

Copy link
Collaborator

commented Sep 1, 2016

Would this cop encourage using stub_const or would it just discourage constants entirely?

@bquorning

This comment has been minimized.

Copy link
Member Author

commented Sep 1, 2016

I don’t know. In most cases you could probably just use local variables – e.g.

foo = Class.new do
  def self.bar
    1
  end
end
puts foo.bar
#=> 1

but we should probably allow using stub_const as well.

@backus

This comment has been minimized.

Copy link
Collaborator

commented Sep 1, 2016

Sounds like a good idea

@backus

This comment has been minimized.

Copy link
Collaborator

commented Sep 1, 2016

This is a tricky one to get right but seems like a good idea

@bquorning

This comment has been minimized.

Copy link
Member Author

commented Nov 18, 2016

pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 20, 2019

Improve examples for LeakyConstantDeclaration cop
As mentioned here
rubocop-hq#765 (comment),
it's not even necessary in some cases to stub a constant, a so-called
"sacrificial class" will do.
Originally mentioned article
http://blog.bitwrangler.com/2016/11/10/sacrificial-test-classes.html in
this comment rubocop-hq#197 (comment)

pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 20, 2019

Improve examples for LeakyConstantDeclaration cop
As mentioned here
rubocop-hq#765 (comment),
it's not even necessary in some cases to stub a constant, a so-called
"sacrificial class" will do.
Originally mentioned article
http://blog.bitwrangler.com/2016/11/10/sacrificial-test-classes.html in
this comment rubocop-hq#197 (comment)
@pirj pirj referenced this issue Jun 20, 2019
3 of 3 tasks complete

pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 23, 2019

Improve examples for LeakyConstantDeclaration cop
As mentioned here
rubocop-hq#765 (comment),
it's not even necessary in some cases to stub a constant, a so-called
"sacrificial class" will do.
Originally mentioned article
http://blog.bitwrangler.com/2016/11/10/sacrificial-test-classes.html in
this comment rubocop-hq#197 (comment)

pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 23, 2019

Improve examples for LeakyConstantDeclaration cop
As mentioned here
rubocop-hq#765 (comment),
it's not even necessary in some cases to stub a constant, a so-called
"sacrificial class" will do.
Originally mentioned article
http://blog.bitwrangler.com/2016/11/10/sacrificial-test-classes.html in
this comment rubocop-hq#197 (comment)

pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 28, 2019

Improve examples for LeakyConstantDeclaration cop
As mentioned here
rubocop-hq#765 (comment),
it's not even necessary in some cases to stub a constant, a so-called
"sacrificial class" will do.
Originally mentioned article
http://blog.bitwrangler.com/2016/11/10/sacrificial-test-classes.html in
this comment rubocop-hq#197 (comment)

pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 28, 2019

Improve examples for LeakyConstantDeclaration cop
As mentioned here
rubocop-hq#765 (comment),
it's not even necessary in some cases to stub a constant, a so-called
"sacrificial class" will do.
Originally mentioned article
http://blog.bitwrangler.com/2016/11/10/sacrificial-test-classes.html in
this comment rubocop-hq#197 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.