# MiniTest: a curious case of include (1.8 vs 1.9)

**URL:** <https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937>\
**Category:** Ruby\
**Created:** [January 29, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937 "2009-01-29T16:16:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon780](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@simon780](https://www.ruby-forum.com/u/simon780)\
**Post date:** [January 29, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937/1 "2009-01-29T16:16:17Z")

</div>

I’ve been messing around with minitest and came across something very  
strange. Does anyone have an explanation for what is going on here?  
I think it must indicate subtle differences in blocks between 1.8 and  
1.9.

require ‘rubygems’  
require ‘minitest/spec’

# 

# Including a module in a spec does not make constants

# available under ruby 1.8 but it does under ruby 1.9.

# 

# This script requires the minitest gem to be installed:

# 

# % gem install minitest

# 

module A  
module B  
end  
end

# This passes on ruby 1.9, but not on 1.8.

describe “MiniTest Includes” do  
include A

```
it "should show inclusion works" do
  B.must_equal A::B
end

```

end

# This passes on both. What’s more, if you uncomment this

# spec, the assignment TRANSLATES to the “MiniTest Includes”

# spec and that spec will pass!

# 

# describe “MiniTest Assigns” do

# B = A::B

# 

# it “should show assignment works” do

# B.must\_equal A::B

# end

# end

# Here is an example showing the assignment case works.

module X  
module Y  
end  
end

describe “MiniTest Assigns X::Y” do  
Y = X::Y

```
it "should show constant assignment works" do
  Y.must_equal X::Y
end

```

end

MiniTest::Unit.autorun

---

<div class="post-metadata">

**Author:** ![simon780](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@simon780](https://www.ruby-forum.com/u/simon780)\
**Post date:** [January 29, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937/2 "2009-01-29T17:08:24Z")

</div>

As an additional note to this, assigning constants in a describe under  
1.8 causes namespace pollution.

require ‘rubygems’  
require ‘minitest/spec’

module X  
module Y  
end  
end

describe “Namespace Pollution” do  
Y = X::Y  
end

puts Y # =\> ‘X::Y’ on 1.8, error on 1.9

---

<div class="post-metadata">

**Author:** ![simon780](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@simon780](https://www.ruby-forum.com/u/simon780)\
**Post date:** [January 29, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937/3 "2009-01-29T17:24:26Z")

</div>

Hi –

On Fri, 30 Jan 2009, Simon C. wrote:

> describe “Namespace Pollution” do  
> Y = X::Y  
> end
> 
> puts Y # =\> ‘X::Y’ on 1.8, error on 1.9

It’s not specific to minitest; it’s a change in how constants are  
resolved. I’m not sure of the exact formulation but what’s happening  
is that in a class\_eval’d code block (which I think is what describe  
does), 1.8 is looking for a constant scoped to the outer level, while  
1.9 is looking for one that is scope to the class that’s being  
class\_evaled.

You can see this also with a block for a new class:

module A  
module B  
end  
end

Class.new do  
include A  
p B == A::B # true in 1.9, error in 1.8 (unknown constant B)  
end

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![simon780](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@simon780](https://www.ruby-forum.com/u/simon780)\
**Post date:** [January 29, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937/4 "2009-01-29T19:54:21Z")

</div>

> It’s not specific to minitest; it’s a change in how constants are  
> resolved. I’m not sure of the exact formulation but what’s happening  
> is that in a class\_eval’d code block (which I think is what describe  
> does), 1.8 is looking for a constant scoped to the outer level, while  
> 1.9 is looking for one that is scope to the class that’s being  
> class\_evaled.

Interesting. I expected it would be something like this. Kinda makes  
specs hard to use under 1.8 IMO. You’re left with nesting describes  
within a module, or using the full constant name.

module A  
module B  
end

```
describe "Nest in Module" do
  it "should show nesting works" do
    B.must_equal A::B
  end
end

```

end

describe “Full Constant Name” do  
it “should show full names works” do  
A::B.must\_equal A::B  
end  
end

---

<div class="post-metadata">

**Author:** ![simon780](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@simon780](https://www.ruby-forum.com/u/simon780)\
**Post date:** [January 29, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937/5 "2009-01-29T21:40:46Z")

</div>

Hi –

On Fri, 30 Jan 2009, Simon C. wrote:

> within a module, or using the full constant name.  
> end
> 
> describe “Full Constant Name” do  
> it “should show full names works” do  
> A::B.must\_equal A::B  
> end  
> end

All it means is that you can’t make an assertion in the spec that  
conflicts with how the language is engineered, and that’s always true  
🙂

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![simon780](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@simon780](https://www.ruby-forum.com/u/simon780)\
**Post date:** [February 2, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937/6 "2009-02-02T21:05:24Z")

</div>

Here’s a workaround that combines the include call with a const\_get  
call. Can still use ‘describe’. Doesn’t pollute namespace. Works on  
1.8 and 1.9. Not too ugly:

require ‘minitest/spec’  
MiniTest::Unit.autorun

# put this little method in some kind of spec\_helper.rb file

module Kernel  
def use(const, subconst)  
self.instance\_eval( “include #{const}” )  
const.const\_get(subconst)  
end  
end

module A  
module B  
end  
end

describe ‘Includes’ do  
B = use A, ‘B’

```
it 'shows inclusion working' do
  B.must_equal A::B
end

```

end

---

<div class="post-metadata">

**Author:** ![simon780](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@simon780](https://www.ruby-forum.com/u/simon780)\
**Post date:** [January 30, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/minitest-a-curious-case-of-include-1-8-vs-1-9/158937/7 "2009-01-30T18:54:57Z")

</div>

> All it means is that you can’t make an assertion in the spec that  
> conflicts with how the language is engineered, and that’s always true  
> 🙂

I guess there’s something to that. Here’s a good workaround that  
doesn’t pollute and works on 1.8 and 1.9… evade the block by  
foregoing describe.

module A  
module B  
end  
end

# This passes on both ruby 1.8 and 1.9 and doesn’t pollute.

class MiniTestIncludes \< MiniTest::Spec  
include A

```
it "should show inclusion works" do
  B.must_equal A::B
end

```

end
