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 に答える