未定义的方法,rails 4.1.9,rspec 3.1

undefined method, rails 4.1.9, rspec 3.1

我有以下模块:

module BlackBird
  module PublishToSites
    module User

      @site = nil

      @params = nil

      @times = 5

      def self.publish_user(params, request_url)
        if Site.all != nil
          threads = []
          Site.all.each do |s|
            @site = s
            @params = params
            threads << Thread.new do
              responses = rescue_500(5) { publish(s, params) }
              deal_with_response(s, responses, request_url)
            end
          end

          return threads
        else
          # Send Email
          #  - Use No Sites Template
        end
      end

      private

      def self.publish(site, params)
        response = Typhoeus.post(
        site.site_api_url + 'users',
        :body => params.to_json,
        :headers => {
            "Authorization" => "Token token=\"#{site.site_api_key}\"",
            'Content-Type' => 'application/json'
          }
        )
        return response
      end

      def self.rescue_500(n , &block)
        responses = []
        (n-1).times {
          response = block.call
          if response.code == 500
            responses << response
            sleep 15
          else
            responses << response
            return responses.uniq(&:code)
          end
        }

        responses << block.call
        return responses.uniq(&:code)
      end

      def deal_with_response(site, responses, request_url)
        response.each do |response|
          if response.code == 200
            send_request(site.site_api_url, site.site_api_key, response, request_url)
          elsif response.code == 404
            send_request(site.site_api_url, site.site_api_key, response, request_url)
            GeneralMailer.recieved_404(ENV['ADMINISTRATOR_NAME'], ENV['ADMINISTRATOR_EMAIL'], site, response).deliver_now
          elsif response.code == 500
            send_request(site.site_api_url, site.site_api_key, response, request_url)
            GeneralMailer.recieved_500(ENV['ADMINISTRATOR_NAME'], ENV['ADMINISTRATOR_EMAIL'], site, response).deliver_now
          else
            send_request(site.site_api_url, site.site_api_key, response, request_url)
            GeneralMailer.recieved_error(ENV['ADMINISTRATOR_NAME'], ENV['ADMINISTRATOR_EMAIL'], site, response).deliver_now
          end
        end
      end

      def send_request(site, api_key, response, from_site)
        # do something here.
      end
    end
  end
end

它包含以下方法,在 private 下称为 deal_with_response(site, responses, request_url)

它的 "private" 不会导致此错误,因为我还有其他测试 rescue_500 方法的测试。

以下测试:

it "should send no email (200)" do
  response = double("response", :code => 200)
  site = double("site", :site_api_url => 'http://google.ca', site_api_key: 'sdsadsasada')
  send_request_method = double()
  send_request_method.stub(:send_request).with(site.site_api_url, site.site_api_key, [response], 'http://google.ca').and_return nil

  expect{ BlackBird::PublishToSites::User.deal_with_response(site.site_api_url, response, 'http://google.ca') }.to_not change{ ActionMailer::Base.deliveries.count }
end

由于以下事实而失败:

1) BlackBird::PublishToSites::User Test that emails are being sent should send no email (200)
     Failure/Error: expect{ BlackBird::PublishToSites::User.deal_with_response(site.site_api_url, response, 'http://google.ca') }.to_not change{ ActionMailer::Base.deliveries.count }
     NoMethodError:
       undefined method `deal_with_response' for BlackBird::PublishToSites::User:Module
     # ./spec/blackbird/publish_to_sites/user_spec.rb:129:in `(root)'
     # ./spec/blackbird/publish_to_sites/user_spec.rb:129:in `(root)'

本例中的第 129 行是:

expect{ BlackBird::PublishToSites::User.deal_with_response(site.site_api_url, response, 'http://google.ca') }.to_not change{ ActionMailer::Base.deliveries.count }

如何:deal_with_response 未定义此模块?我错过了什么吗?打字错误?我看了 5 遍,我调用了正确的方法....

模块中的其他方法被定义为模块方法(def self.method_name),因此无需封装对象即可在模块本身上调用。 deal_with_response 方法不是以这种方式定义的,因此它是一个实例方法,当包含模块时,它只能作为 class 上的实例方法调用。

Source.