簡體   English   中英

如何使用RSpec在我的Rails應用程序中測試JQuery-UI Sortable的控制器交互?

[英]How do I test JQuery-UI Sortable's controller interaction in my Rails app using RSpec?

問題

問題是我知道正在調用控制器排序操作,並且它正在獲取正確的參數來執行其操作。 通過UI進行測試時,我可以看到一切正常,但是無法進行測試 我可以看到(在控制器操作中使用“ puts”)哈希值已正確發送到控制器操作,因此可以正常工作。 問題在於,似乎控制器的動作根本就不是... ACTING。

問題

我該如何測試? 我嘗試在規范中的:post,:sort行之后插入“ sleep 2”。 您可以看到我已經在該行的前面添加了“ xhr”,並且嘗試在該行的末尾添加:format =>:js(未顯示)無濟於事。

我在做什么

我正在使用JQuery UI的“可排序”插件在屏幕上的清單上對作業進行排序。 當前它運行良好,我可以通過更改屏幕順序來“測試”,然后刷新頁面以查看所有內容是否位於正確的位置(刷新之前我剛剛將內容移至該位置)。 顯然,這不是嚴格的測試,但是到目前為止已經足夠了。

現在,我添加了一些邏輯來在后台復制/歸檔已排序的項目,並且我需要進行一些測試以確保在執行排序時所有數據都被正確地操縱。 舊的排序刷新檢查測試方法在這里不起作用,因為某些東西在后台發生,並且這些東西沒有顯示在屏幕上。 在編寫這些測試之前,我想為當前正在使用的設置編寫測試,但是我無法通過這些測試。

關聯說明

有作業,檢查表和檢查表工作(聯接表)。 聯接表具有“ job_position”字段,正在使用這種排序方式對其進行更新。 有關模型的摘要,請參見下面的“代碼”部分。

用於執行此操作的資源

jQuery UI-可排序

Railscast#147可排序列表(修訂版)

守護寶石

導軌3.2.11

紅寶石1.9.3p374

規范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

job.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

這是一個非常簡單的答案:我在before塊中構建的@attr數組是作業ID的數組,應該是checklists_job ID的數組。

這個:

@attr = [@job4.id, @job3.id, @job2.id, @job1.id] 

應該是這樣的:

@attr = [@cj4.id, @cj3.id, @cj2.id, @cj1.id]

哎呀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM