1

これが私のコントローラーテストです(「spec/controllers/api/tasks_controller_spec.rb」)

require 'spec_helper'

describe Api::TasksController do
    before :each do
        @quadros = create :cat
        @task = Task.new(:content => "Example task for #api")
        @quadros.add_task(@task)
    end

    describe "GET index" do
        it "returns a list of all user's tasks" do
            get :index, :format => 'json'
            expect(response).to eq(User.first.all_tasks)
        end 
    end
end

ここに私の Api::BaseController があります

class Api::BaseController < ApplicationController
    respond_to :json
    skip_before_filter :authenticate_user!
end

そして Api::TaskController

class Api::TasksController < Api::BaseController
    def index
        @tasks = User.first.all_tasks
        respond_with @tasks.to_json
    end
end

私の他のテストは正常に実行されます。

API テストを実行すると、before ブロックが実行され、リクエストが json として作成され、次のクエリでハングします。

Processing by Api::TasksController#index as JSON
  User Load (0.3ms)  SELECT `users`.* FROM `users` LIMIT 1
  Tag Load (0.3ms)  SELECT `tags`.* FROM `tags` WHERE (user_id = 418 AND parent_tag_id IS NOT NULL)
  Tag Load (0.2ms)  SELECT `tags`.* FROM `tags` WHERE `tags`.`id` IN (NULL)
  Tag Load (0.3ms)  SELECT `tags`.* FROM `tags` WHERE (user_id = 418 AND parent_tag_id IS NULL)
  Task Load (0.7ms)  SELECT tasks.* FROM `tasks` JOIN tag_tasks on tasks.id = tag_tasks.task_id WHERE (tag_tasks.tag_id IN (301) OR creator_id = 418) GROUP BY tasks.id ORDER BY tasks.created_at DESC
Completed 200 OK in 99ms (Views: 0.1ms | ActiveRecord: 0.0ms)
  User Load (0.3ms)  SELECT `users`.* FROM `users` LIMIT 1
  Tag Load (0.2ms)  SELECT `tags`.* FROM `tags` WHERE (user_id = 418 AND parent_tag_id IS NOT NULL)
  Tag Load (0.2ms)  SELECT `tags`.* FROM `tags` WHERE `tags`.`id` IN (NULL)
  Tag Load (0.2ms)  SELECT `tags`.* FROM `tags` WHERE (user_id = 418 AND parent_tag_id IS NULL)
  Task Load (0.7ms)  SELECT tasks.* FROM `tasks` JOIN tag_tasks on tasks.id = tag_tasks.task_id WHERE (tag_tasks.tag_id IN (301) OR creator_id = 418) GROUP BY tasks.id ORDER BY tasks.created_at DESC

永遠に座る場所。

なぜこれが起こっているのでしょうか?

4

1 に答える 1

1

同様の問題に遭遇しましたが、問題はあなたの期待ラインにあるようです:

expect(response).to eq(User.first.all_tasks)

これは、RSpec が応答本文をテストする方法ではありません。docsでは、等値マッチャーの代わりに特殊なマッチャーが使用されていることに注意してください。

expect(response).to render_template("index")

したがって、であるresponseオブジェクトは、応答本文が何であったかではなく、何が起こったかActionController::TestResponseについて照会されることを意図しています。したがって、テストは次のようになります。

expect(JSON.parse(response.body)).to eq(User.first.all_tasks)

(応答本文は文字列であることに注意してください。)

テストが完全に失敗するのではなくハングする理由の説明については、次のコード ブロック ( lib/rspec/expectations/fail_with.rb:22gemrspec-expectationsバージョン 2.14.0 内) が原因のようです。

if actual && expected
  if all_strings?(actual, expected)
    if any_multiline_strings?(actual, expected)
      message << "\nDiff:" << differ.diff_as_string(coerce_to_string(actual), coerce_to_string(expected))
    end
  elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
    message << "\nDiff:" << differ.diff_as_object(actual, expected)
  end
end

、、、およびメソッド (同じファイルで定義) はすべて、 で呼び出さall_strings?れます。この場合、問題は であることがわかります。これにより、実行時エラーが発生せずにメソッド自体がハングします。これをさらに調査する時間はありませんでしたが、興味のある方はソースがここにあります。any_multiline_strings?no_procs?no_numbers?args.flatten[actual, expected]actualTestResponseflattenArray.flatten

于 2014-02-27T20:02:06.723 に答える