rubyrspec2

Include module in rspec


Please forgive my gherkin

Given I have a module called Hello
And it has a method called world and a class called World
When I include the module in an RSpec spec
And I call the method world
Then it returns "hello world from method"
But when I create a World class
Then I get NameError: uninitialized constant World

module Hello
  def world
    p "hello world from method"
  end
  class World
     def initialize
       p "hello world from class"
     end
  end
end

describe "hello world" do
  include Hello
  it "call method" do
    world
  end

  it "call class" do
    World.new
  end
end

Solution

  • Forgive my Gherkin, and unless you intended something else (as that's not clear)

    When I create a Hello::World class  
    Then I get an object of kind Hello::World
    
    it "should not catch fire on instantiation" do
      Hello::World.new.should_not be_nil
    end