kucaahbe / rspec-html-matchers

Old school have_tag, with_tag(and more) matchers for rspec 3 (Nokogiri powered)
http://rubygems.org/gems/rspec-html-matchers
MIT License
199 stars 90 forks source link

Use a module level variable instead of @__current_scope_for_nokogiri_matcher #22

Closed sshaw closed 7 years ago

sshaw commented 11 years ago

Hi, I'm trying to do the following:

class HaveField
    include RSpec::Matchers # just for your methods

    def initialize(name, &block)
      @name = name
      @block = block
    end

    def matches?(target)
      @target = target
      @target.should have_tag("div.control-group") do
         with_tag "label", :with => { :class => "control-label", :for => "item_#@name" }, :text => "#{@name.to_s.titleize}: "
         with_tag "div.controls", &@block
      end
    end

   # ...
end

#...

describe "#text_field" do
  it "creates the text field" do
    builder.text_field(:name).should have_field(:name) {
      with_tag "input#item_name", :with => { :name => "item[name]", :type => "text", :value => "sshaw" }
    }
  end
end

But, I get this error:

  NoMethodError:
       undefined method `current_scope' for nil:NilClass
     # /Users/sshaw/.rvm/gems/ruby-1.9.2-p290/gems/rspec-html-matchers-0.4.1/lib/rspec-html-matchers.rb:104:in `matches?'

Since @__current_scope_for_nokogiri_matcher was nil when the block argument to have_field(:name) was created the subsequent call to have_tag using that block doesn't work as the var is still nil in the block's binding.

Any reason to use RSpec's namespace?

Instead of using an instance var within RSpec::Matchers how do you feel about adding the matchers to their own module and getting the current scope form a module method RSpec::HTML::Matchers.current_scope?

If that's OK I can submit a patch.

kucaahbe commented 11 years ago

I've got the problem, of course I would like to see a patch, thanks.

randoum commented 7 years ago

Too old. Closing