2013-06-12 198 views
0

這裏是我的控制器測試( 「投機/控制器/ API/tasks_controller_spec.rb」)Rspec的控制器測試掛

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測試,它執行塊之前,發出請求爲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 

凡只會永遠地坐着。

任何想法,爲什麼這可能發生?

回答

1

我已經遇到了類似的問題,它出現的問題是與你的預期一致:

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

這不是RSpec的是如何讓你在測試響應主體。請注意,in the docs,專業的匹配使用,而不是平等匹配:

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

所以response對象,這是一個ActionController::TestResponse,意味着要查詢有關發生了什麼,沒有什麼反應身體。所以,你的測試應該是這樣的:

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

(請注意,響應體是一個字符串)。

至於爲什麼測試的解釋掛起,而不是完全沒有,看來這個塊(在rspec-expectations寶石版本2.14.0 lib/rspec/expectations/fail_with.rb:22)碼是罪魁禍首:

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?方法(d在同一個文件中定義)都在[actual, expected]上調用args.flatten。在這種情況下,從我可以告訴問題是actualTestResponse,這會導致flatten方法本身掛起而不會引起運行時錯誤。我沒有時間進一步調查,但如果有人感興趣,Array.flatten的來源是here