問題如何使用RSpec在我的Rails應用程序中測試JQuery-UI Sortable的控制器交互?
的問題是,我知道控制器排序操作被調用,而且它得到正確的PARAMS做它的事。當我通過用戶界面進行測試時,我可以看到一切正常,但是對於我的測試,它不會排序。我可以看到(在控制器操作中使用「puts」)散列正在發送到控制器操作,因此運行良好。問題在於,控制器的行爲似乎不是...... ACTING。
問題
如何測試呢?我試過在我的規範中的post::sort行之後插入一個「sleep 2」。你可以看到我已經在該行的前面添加了「xhr」,並且我嘗試在該行的末尾添加:format =>:js(沒有顯示),但無濟於事。
我使用jQuery UI的「排序」插件我在做什麼
對屏幕上的清單作業進行分類。它目前運行良好,我可以通過更改屏幕上的順序來「測試」,然後刷新頁面以查看所有內容現在都位於正確的位置(在刷新之前,我只是將它移動到了位置)。顯然,這不是嚴格的測試,但直到現在它已經足夠好了。
現在,我添加了一些邏輯來在後臺複製/存檔已排序的項目,我需要做一些測試以確保在進行排序時正確處理所有數據。舊的排序 - 刷新檢查測試方法在這裏不起作用,因爲有些東西發生在後臺,並且這些東西沒有顯示在屏幕上。在我編寫這些測試之前,我想爲當前工作的設置編寫測試,但是我無法通過測試。
注意事項協會
有喬布斯,清單和ChecklistsJobs(連接表)。連接表具有使用此類更新的「job_position」字段。請參閱下面的「代碼」部分中的模型片段。
資源用來做這
Railscast #147 Sortable Lists (revised)
的Rails 3.2.11
紅寶石1.9.3p374
rspec的2.13.1
寶石在:測試
gem "rspec-rails", :group => [:test, :development]
group :test do
gem "factory_girl_rails", :require => false
gem "capybara"
gem "guard-rspec"
gem 'rb-fsevent', '~> 0.9'
end
代碼
工作。RB
class Job < ActiveRecord::Base
scope :archived_state, lambda {|s| where(:archived => s)}
belongs_to :creator, :class_name => "Admin", :foreign_key => "creator_id"
has_many :checklists_jobs, :dependent => :destroy
has_many :checklists, :through => :checklists_jobs
.
.
.
end
checklist.rb
class Checklist < ActiveRecord::Base
scope :archived_state, lambda {|s| where(:archived => s) }
has_many :checklists_jobs, :dependent => :destroy, :order => 'checklists_jobs.job_position'#, :conditions => {'archived_at' => nil}
has_many :jobs, :through => :checklists_jobs
.
.
.
end
checklists_job.rb
class ChecklistsJob < ActiveRecord::Base
belongs_to :job
belongs_to :checklist
attr_accessible :job_position, :job_required
.
.
.
end
的application.js
$(function() {
$('ul[id^="sortable"]').sortable({
axis: 'y',
handle: '.handle',
update: function() {
return $.post($(this).data('update-url'), $(this).sortable('serialize'));
}
});
});
Show.html.erb視圖
<dd><ul class="unstyled" id="sortable" data-update-url="<%= sort_checklist_path(@checklist) %>">
<% @checklist.checklists_jobs.archived_state(:false).each do |cj| %>
<%= content_tag_for :li, cj do %><span class="handle"><i class="icon-move btn btn-mini"></i></span> <strong><%= cj.job.name %></strong><% if cj.job.description? %> - <%= cj.job.description %><% end %>
<% end %>
<% end %>
</ul></dd>
checklists_controller.rb
def sort
@checklist = Checklist.find(params[:id])
params[:checklists_job].each_with_index do |id, index|
@checklist.checklists_jobs.update_all({job_position: index+1}, {id: id})
end
render nothing: true
end
checklists_controller_spec.rb - 我知道這是不是空間超高效的用戶,但我打破了一切爲三個獨立的線路,以確保我正確地設置了一切。
require 'spec_helper'
describe ChecklistsController do
login_admin
describe "POST sort" do
context "with no submission" do
before do
@checklist = FactoryGirl.create(:checklist)
@job1 = FactoryGirl.create(:job)
@job2 = FactoryGirl.create(:job)
@job3 = FactoryGirl.create(:job)
@job4 = FactoryGirl.create(:job)
@checklist.jobs << [@job1, @job2, @job3, @job4]
@checklist.save
@cj1 = @checklist.checklists_jobs.find_by_job_id(@job1.id)
@cj2 = @checklist.checklists_jobs.find_by_job_id(@job2.id)
@cj3 = @checklist.checklists_jobs.find_by_job_id(@job3.id)
@cj4 = @checklist.checklists_jobs.find_by_job_id(@job4.id)
@cj1.job_position = 1
@cj2.job_position = 2
@cj3.job_position = 3
@cj4.job_position = 4
@cj1.save
@cj2.save
@cj3.save
@cj4.save
@attr = [@job4.id, @job3.id, @job2.id, @job1.id]
end
# format of the params hash that's being passed:
# {"checklists_job"=>["545", "544", "546", "547"], "id"=>"124"}
it "sorts the checklist's checklists_jobs" do
xhr :post, :sort, { :id => @checklist.id, :checklists_job => @attr }
@checklist.reload
# The test fails on the next line
@checklist.checklists_jobs.find_by_job_id(@job1.id).job_position.should == 4
end
end
end
end
測試結果 - 請注意,我已經標誌着規範的失敗線以上
Failures:
1) ChecklistsController POST sort with no submission sorts the checklist's checklists_jobs
Failure/Error: @checklist.checklists_jobs.find_by_job_id(@job1.id).job_position.should == 4
expected: 4
got: 1 (using ==)
# ./spec/controllers/checklists_controller_spec.rb:394:in `block (4 levels) in <top (required)>'
Finished in 2.59 seconds
51 examples, 1 failure
Failed examples:
rspec ./spec/controllers/checklists_controller_spec.rb:391 # ChecklistsController POST sort with no submission sorts the checklist's checklists_jobs