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::TasksController

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:22rspec-expectationsgem 版本 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?调用on 。在这种情况下,据我所知,问题是a ,这会导致方法本身挂起而不会引发运行时错误。我没有时间对此进行进一步调查,但如果有人感兴趣,来源就在这里。no_procs?no_numbers?args.flatten[actual, expected]actualTestResponseflattenArray.flatten

于 2014-02-27T20:02:06.723 回答