0

下面是我正在运行的一个 rspec 测试,以测试我制作的另一个类。不幸的是,我尝试测试的方法 ( delete) 似乎不起作用。让我感到震惊的是,我从 Termianl 收到的错误消息是:

/Users/user/Ruby/localWikiClient/localwiki_client/spec/delete_spec:11:in 'block (2 levels) in <top (required)>': undefined method 'delete' for #<Proc:0x007fe4739a5448> (NoMethodError)

但是,此方法是在类中定义的。下面是代码:

require 'faraday'
require 'json/pure'

module Localwiki

  ##
  # A client that wraps the localwiki api for a given server instance
  #
  class Client

    attr_accessor :hostname       # hostname of the server we'd like to point at
    attr_reader   :site_name      # site resource - display name of wiki
    attr_reader   :time_zone      # site resource - time zone of server, e.g. 'America/Chicago'
    attr_reader   :language_code  # site resource - language code of the server, e.g. 'en-us'

    def initialize hostname, user=nil, apikey=nil
      @hostname = hostname
      @user = user
      @apikey = apikey
      create_connection
      collect_site_details
    end

   ##
   # Get site resource and set instance variables
   #
   def collect_site_details
       site = fetch('site','1')
       @site_name = site['name']
       @time_zone = site['time_zone']
       @language_code = site['language_code']
   end

   ##
   # create Faraday::Connection instance and set @site
   #
   def create_connection
      @site = Faraday.new :url => @hostname
   end

   ##
   # delete a specific resource
   # resources are "site", "page", "user", "file", "map", "tag", "page_tag"
   # identifier is id, pagename, slug, etc.
  def delete(resource,identifier)
    case resource
    when resource == "site"
      @hostname = identifier
      create_connection
    when resouce == "user"
      @hostname = list(identifier)   
    end  

    http_delete()
  end

  def http_delete()
    response = @site.delete
    puts response.to_s
  end

这是我正在尝试运行的 rspec 测试:

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require 'localwiki_client'

describe '<siteinfo>.amazonaws.com/bears' do

  subject { Localwiki::Client.new '<siteinfo>.compute-1.amazonaws.com/bears', '<username>', '[myApiKey]' }

  context '#fetch' do
    subject.delete('page', 'bears')
  end

end
4

1 回答 1

4

您不能在上下文块中访问这样的主题。您将需要将它放在之前的块中或实际的测试块中(它/指定):

describe '<siteinfo>.amazonaws.com/bears' do

  subject { Localwiki::Client.new '<siteinfo>.compute-1.amazonaws.com/bears', '<username>', '[myApiKey]' }

  context '#fetch' do
    it "deletes the bears page" do
      subject.delete('page', 'bears')
    end
  end

end
于 2013-02-27T04:07:26.230 回答