0

After including a module in spec_helper, I am running Minitest spec for a class inside that module and get this error:

test_0001_must be true for option name(MyGem::OptionParser::option?):
NoMethodError: undefined method `option?' for OptionParser:Class

I'm testing lib/options/options.rb:

module MyGem
  class OptionParser
    def self.option?(arg)
      arg =~ /^-{1,2}\w+$/
    end
  end
end

With spec/options_spec.rb:

describe OptionParser do
  describe "option?" do
    it "must be true for option name" do
      OptionParser.option?('--nocolor').must_equal true
    end
  end
end

Running the test with MyGem::OptionParser instead of just OptionParser doesn't cause errors. But similar test on lib/script.rb runs without errors without MyGem:: prefix.

My file structure:

gem/
|-lib/
| |-options/
| | |-options.rb
| |-script.rb
|-spec/
| |-script_spec.rb
| |-options_spec.rb
| |-spec_helper.rb
|-Rakefile

I include MyGem in spec_helper. What have I got wrong?

leemour
  • 11,414
  • 7
  • 36
  • 43
  • Hi again, leemour. Didn't my comment in your previous question solve this? Anyway, how does your 'spec_helper' look like? – Gjaldon Dec 15 '13 at 17:44
  • Hi, Gjaldon. I still had an issue after your comment, so I created a new question. – leemour Dec 15 '13 at 19:31

2 Answers2

1

Minitest either already includes or auto loads Ruby's own OptionParser, so that presumably is taking precedence and preventing the loading of your version. Here's evidence:

MacbookAir1:so1 palfvin$ irb
2.0.0p247 :001 > OptionParser
NameError: uninitialized constant OptionParser
    from (irb):1
    from /Users/palfvin/.rvm/rubies/ruby-2.0.0-p247/bin/irb:16:in `<main>'
2.0.0p247 :002 > require 'minitest'
 => true 
2.0.0p247 :003 > OptionParser
 => OptionParser 
2.0.0p247 :004 > 
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
  • I am not using Rails. By similar test I mean testing a method of MyGem::OtherClass in `lib/script.rb`. – leemour Dec 15 '13 at 19:12
  • You gave me a very good tip for `OptionParser` reserved name. After renaming it to `OptionsParser` the tests worked. Thanks a lot! I just don't understand, why there is a name conflict. `OptionParser` comes with Ruby http://ruby-doc.org/stdlib-2.0.0/libdoc/optparse/rdoc/OptionParser.html but I didn't find where it is required by Minitest. – leemour Dec 15 '13 at 19:25
  • Just wanted to clarify that in my example where I included the module, contained classes are accessible without specifying the module prefix. Can you give an example when your statement is true? – leemour Dec 15 '13 at 19:37
  • @leemour I was off base with that comment. I still haven't managed to get my head satisfactorily around modules. Sorry for misleading you in that regard. I've updated my answer. – Peter Alfvin Dec 15 '13 at 23:30
0

If you have exactly the same MyGem::OptionParser defined in your script.rb file, that is likely what is causing issues in your spec. Try using another namespace for your code in options.rb like so:

module MyOtherGem
  class OptionParser
    def self.option?(arg)
      arg =~ /^-{1,2}\w+$/
    end
  end
end

Then make sure to include it in your spec_helper or in your options_spec file.

Gjaldon
  • 5,534
  • 24
  • 32
  • Sorry, I just realized I inadvertently downvoted your answer, particularly pathetic since mine was wrong. ;-) If you'll edit it, I'll remove the downvote. – Peter Alfvin Dec 15 '13 at 23:39