[rspec-devel] [ rspec-Feature Requests-7613 ] context#include should add constants into the context

noreply at rubyforge.org noreply at rubyforge.org
Thu Feb 8 06:35:36 EST 2007


Feature Requests item #7613, was opened at 2007-01-06 01:25
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=7613&group_id=797

Category: None
Group: None
Status: Open
Priority: 3
Submitted By: Brian Takita (btakita)
Assigned to: Nobody (None)
Summary: context#include should add constants into the context

Initial Comment:
dir = File.dirname(__FILE__)
require "#{dir}/spec_helper"

module Foo
  class Bar
  end
end

context "A Context include statement" do
  include Foo
  specify "should add constants accessable in the context" do
    b = Bar.new
  end
end


----------------------------------------------------------------------

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-02-08 11:35

Message:
I've narrowed this down to our own use of instance_eval when running specs. Apparently Ruby doesn't allow access to instance constants from instance eval:

irb(main):001:0>   class Foo
irb(main):002:1>     BAR = "bar"
irb(main):003:1>     def get_bar
irb(main):004:2>       BAR
irb(main):005:2>     end
irb(main):006:1>   end
=> nil
irb(main):007:0> foo = Foo.new
=> #<Foo:0x10d90c0>
irb(main):008:0> foo.instance_eval { "bar" }
=> "bar"
irb(main):009:0> foo.instance_eval { get_bar }
=> "bar"
irb(main):010:0> foo.instance_eval { BAR }
NameError: uninitialized constant BAR
        from (irb):10
        from (irb):10:in `instance_eval'
        from (irb):10



----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-02-08 10:56

Message:
Seems like it!

FUN!

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-02-08 10:33

Message:
Are we facing the challenge of reimplementing Ruby's class system here?

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-02-08 05:21

Message:
This gets even better:

  module Helpers
    CONSTANT = "constant"
    class Helper
    end
  end
  
  context "context#include" do
    include Helpers
    
    specify "should include qualified constants" do
      Helpers::CONSTANT.should == "constant"
      Helpers::Helper.new
    end
    
    specify "should include unqualified constants" do
      self.class.constants.should include("CONSTANT")
      CONSTANT.should == "constant"
      Helper.new
    end
    
  end

The second spec bails on CONSTANT.should == "constant", but passes self.class.constants.should include("CONSTANT").

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-02-08 04:11

Message:
>From the ruby-talk list:

module TestHelper
 CONSTANT = "constant"
end

context "A context" do
 include TestHelper

 specify "should allow unqualified access to included constants" do
   CONSTANT.should_eql "constant"
   #fails
 end

 specify "should allow qualified access to all constants" do
   TestHelper::CONSTANT.should_eql "constant"
   # passes w/ fully qualified reference
 end
end

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=7613&group_id=797


More information about the rspec-devel mailing list