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 · Fixed by #765
Closed

Suggestion: Check for constants being defined in specs #197

bquorning opened this issue Sep 1, 2016 · 5 comments · Fixed by #765

Comments

@bquorning
Copy link
Collaborator

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
Copy link
Collaborator

backus commented Sep 1, 2016

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

@bquorning
Copy link
Collaborator Author

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
Copy link
Collaborator

backus commented Sep 1, 2016

Sounds like a good idea

@backus
Copy link
Collaborator

backus commented Sep 1, 2016

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

@bquorning
Copy link
Collaborator Author

Here’s a @kmayer blog post on the issue http://blog.bitwrangler.com/2016/11/10/sacrificial-test-classes.html

pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 20, 2019
As mentioned here
rubocop#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#197 (comment)
pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 20, 2019
As mentioned here
rubocop#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#197 (comment)
pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 23, 2019
As mentioned here
rubocop#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#197 (comment)
pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 23, 2019
As mentioned here
rubocop#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#197 (comment)
pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 28, 2019
As mentioned here
rubocop#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#197 (comment)
pirj added a commit to pirj/rubocop-rspec that referenced this issue Jun 28, 2019
As mentioned here
rubocop#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#197 (comment)
kellysutton pushed a commit to kellysutton/rubocop-rspec that referenced this issue Oct 28, 2019
As mentioned here
rubocop#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#197 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants