NoMethodError when trying to access method defined

2019-06-21 06:44发布

问题:

I'm trying to access a method from a module in one of my spec helpers

I include the module in the test helper

module Support
  class RestHelper
    include Rest::Rest

    def create_rest_client_for_ifa
      # Call method from module
      create_rest_client(uname, pword)
    end
  end
end

But I keep getting a NoMethodError when I run my spec:

Failure/Error: @rest_client = Support::RestHelper.create_rest_client_for_ifa
 NoMethodError:
   undefined method `create_rest_client' for Support::RestHelper:Class

Here is my module code:

module Rest
  module Rest
    .
    .
    def create_rest_client(uname, pword)
      # code
    end
    .
    .
  end
end

It seems to work fine when I test it in the rails console

$ RAILS_ENV=test rails c
irb> include Rest::Rest
=> Object
irb> create_rest_client(uname, pword)

What am I missing? Why can't I access the method from the test helper?

Any help will be much appreciated.

回答1:

As I remember, include adds module methods as instance methods, extend adds them as class methods.