0

以下是我从控制器中搜索的方法

@accounts = []
    client = GameAccounts::GameAccountsClient.new



    if params[:name]


      # Try and find a game account by id using the given name
      response = client.get_accounts_by_name(params[:name])

      if response.is_a?(Net::HTTPSuccess)
        account = client.parse_json(response)
        unless account.empty?
          session[:account] = account
          redirect_to game_account_path(params[:name])
        end
      end

      json = client.get_json(params[:limit],params[:offset],params[:name])

      @presenter = GameAccountsPresenter.new(json)

    end
  end

我正在尝试运行以下测试:

require 'spec_helper'

describe GameAccountsController do
  describe 'GET search' do
    it 'finds a named system account directly' do
     get(:search, name: 'test').to be_redirect_to(game_account_path('test'))
    end
  end
end

我不断得到一个

GameAccountsController GET search finds a named system account directly
     Failure/Error: get(:search, name: 'test').to be_redirect_to(game_account_path('test'))
     NoMethodError:
       undefined method `to' for #<ActionController::TestResponse:0x007f8b0beb3e10>
     # ./spec/controllers/game_accounts_controller_spec.rb:6:in `block (3 levels) in <top (required)>'

谁能让我知道我做错了什么?..尝试做.should redirect_to,我仍然得到同样的错误,但方法是“应该”。

客户端代码

模块 GameAccounts 类 GameAccountsClient

OAUTH_CONFIG=YAML.load_file(Rails.root.join('config','oauth.yml' ))
def consumer
  @consumer ||= OAuth::Consumer.new(OAUTH_CONFIG['oauth_key'],OAUTH_CONFIG['oauth_secret'],access_token_url: OAUTH_CONFIG['oauth_access_token_url'],signature_method: 'HMAC-SHA1')
end

def get_accounts_by_name(name)
   query_account(CGI.escape(name))
end

def get_accounts_by_id(account_id)
  response = query_account(CGI.escape(account_id))
  parse_json(response)
end

def get_json(limit,offset,name)

  limit=set_limit(limit)

  offset = set_offset(offset)

  query = "?name=#{CGI.escape(name)}&limit=#{limit}#{offset}"
  response = query_account(query)
  parse_json(response)
end


def parse_json(response)
   JSON.parse(response.body)
  end
end
4

1 回答 1

1

Rspec 的请求规范可能很棘手。您应该使用参数调用“get”方法,这将创建一个响应方法,其中保存您的实际响应数据:

require 'spec_helper'

describe GameAccountsController do
  describe 'GET search' do
    it 'finds a named system account directly' do
      get(:search, name: 'test')
      response.should redirect_to(game_account_path('test'))
    end
  end
end

希望能帮助到你 :)

编辑:

这是rspec 文档的链接,2.13 版支持该expect语法。上面的例子也可以写成:

require 'spec_helper'

describe GameAccountsController do
  describe 'GET search' do
    it 'finds a named system account directly' do
      get(:search, name: 'test')
      expect(response).to redirect_to(game_account_path('test'))
    end
  end
end

编辑2:

尝试以下代码并测试 get_* 方法的返回值(和类):

def search
  @accounts = []

  if params[:name]

    if get_response.is_a?(Net::HTTPSuccess)
      unless get_account.empty?
        session[:account] = get_account
        redirect_to game_account_path(params[:name])
      end
    end

    json = get_client.get_json(params[:limit],params[:offset],params[:name])
    @presenter = GameAccountsPresenter.new(json)

  end
end

def get_client
  @client ||= GameAccounts::GameAccountsClient.new
end

# Try and find a game account by id using the given name
def get_response
  @response ||= client.get_accounts_by_name(params[:name])
end

def get_account
  @account ||= get_client.parse_json(get_response)
end
于 2013-03-06T19:44:09.937 回答