问题
问题是我知道控制器排序操作正在被调用,并且它得到了正确的参数来做它的事情。当我通过 UI 进行测试时,我可以看到一切正常,但它不会为我的测试排序。我可以看到(在控制器操作中使用“puts”)哈希正在正确地发送到控制器操作,所以效果很好。问题是,控制器动作似乎根本不是...... ACTING。
问题
我该如何测试这个?我尝试在规范中的 :post, :sort 行之后插入“sleep 2”。您可以看到我已经在该行前面添加了“xhr”,并且我尝试在该行的末尾添加 :format => :js (未显示)但无济于事。
我在做什么
我正在使用 JQuery UI 的“可排序”插件对屏幕上的清单上的作业进行排序。它目前运行良好,我可以通过更改屏幕上的顺序来“测试”它,然后刷新页面以查看所有内容现在都在正确的位置(我刚刚在刷新之前将东西移动到的位置)。显然,这不是严格的测试,但到目前为止已经足够好了。
现在,我正在添加一些逻辑来在后台复制/归档排序的项目,并且我需要进行一些测试以确保在进行排序时所有数据都被正确操作。旧的 sort-refresh-check 测试方法在这里不起作用,因为有些事情发生在后台,而这些事情不会出现在屏幕上。在编写这些测试之前,我想为当前工作的设置编写测试,但我无法通过测试。
关联说明
有 Jobs、Checklists 和 ChecklistsJobs(连接表)。连接表具有使用这种排序更新的“job_position”字段。有关模型的片段,请参见下面的“代码”部分。
用于执行此操作的资源
导轨 3.2.11
红宝石 1.9.3p374
rspec 2.13.1
宝石在 :test
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
清单.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
应用程序.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