简体   繁体   中英

Extending modules and testing class (or instance?) methods in Ruby

New to Ruby and thank you in advance! I have 2 files in the same directory. Where.rb =

module Where
  def self.where(hash = {})
    self.select do |fixture| #iterate over fixtures indexes |fixture| 
      hash.all?  do |key, value| #return true as long as hash exists
        value === fixture[key]
      end
    end
  end
end

and test.rb =

require 'minitest/autorun'
require './where.rb'

class WhereTest < Minitest::Test
  extend Where
  def setup
    @boris   = {:name => 'Boris The Blade', :quote => "Heavy is good. Heavy is reliable. If it doesn't work you can always hit them.", :title => 'Snatch', :rank => 4}
    @charles = {:name => 'Charles De Mar', :quote => 'Go that way, really fast. If something gets in your way, turn.', :title => 'Better Off Dead', :rank => 3}
    @wolf    = {:name => 'The Wolf', :quote => 'I think fast, I talk fast and I need you guys to act fast if you wanna get out of this', :title => 'Pulp Fiction', :rank => 4}
    @glen    = {:name => 'Glengarry Glen Ross', :quote => "Put. That coffee. Down. Coffee is for closers only.",  :title => "Blake", :rank => 5}

    @fixtures = [@boris, @charles, @wolf, @glen]
  end

  def test_where_with_exact_match
    assert_equal [@wolf], @fixtures.where(:name => 'The Wolf')
  end

  def test_where_with_partial_match
    assert_equal [@charles, @glen], @fixtures.where(:title => /^B.*/)
  end

  def test_where_with_mutliple_exact_results
    assert_equal [@boris, @wolf], @fixtures.where(:rank => 4)
  end

  def test_with_with_multiple_criteria
    assert_equal [@wolf], @fixtures.where(:rank => 4, :quote => /get/)
  end

  def test_with_chain_calls
    assert_equal [@charles], @fixtures.where(:quote => /if/i).where(:rank => 3)
  end
end

puts WhereTest

all my tests say NoMethodError: undefined method `where' for # Array:0x000000027d6a20.

I'm not sure what the error means. I created a module with a class method that should be usable. The module's method should be available to WhereTest class with extend Where

The error message means that you are trying to call the where method on an instance of Array when there is no such method defined for arrays.

It seems that you want the where method added to the Array class (because you call @fixtures.where and @fixtures is an Array. However, you are adding it, instead, to the WhereTest class.

You would need to do something like:

class Array
  extend Where
end

class WhereTest < Minitest::Test
  def setup
  ...

Now, there are other issues with the code you present (patching a core class is dangerous, for example, and generally strongly recommended against). But this should at least get you a bit closer to what you're trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM