2014-07-18 130 views
3

我想測試是否contenteditable textarea在我的代碼中工作,但我得到一個錯誤,我似乎無法修復。問題是,發送給控制器的參數並不是我期望的AJAX。這裏是我的代碼:水豚Rspec測試可能做到AJAX - 測試contenteditables

rspec的

describe 'editing contenteditables', js: true do 
    before do 
    page.find('#artistdescription').set('Pizza') 
    page.find('#similarArtists').click #in order to blur out of contenteditable 
    end 

    it {should have_content('Pizza')} 
end 

視圖

<% if @is_current_artist %> 
    <% description_text = (@artist.description.blank? && @is_current_artist) ? "test" : @artist.description %> 
    <textarea contenteditable='true' id='artistdescription'> 
    <%= description_text %> 
    </textarea> 
<% else %> 
    ... 

AJAX

$(document).on('blur', '#artistdescription', -> 
    $.ajax(
    url: "/artists/" + $('#idnumber').html() 
    type: "PATCH" 
    datatype: "JSON" 
    data: { 
     artist: { 
     description: $('#artistdescription').val().replace(/(<([^>]+)>)/ig,"") 
     } 
     session_code: getCookie('session_code'); 
    } 
) 
) 

控制器

def update 
    @artist = Artist.find(params[:id]) 
    p 'Hello from update' 
    return if !active_session && !is_current_artist 
    p 'Logged in!' 
    pp artist_params 
    @artist.update_attributes(artist_params) #error here 

    def artist_params 
    #Add more contenteditable stuff here 
    p '=====================TASTY TACOS!' 
    pp params 
    params.require(:artist).permit(:artist_name, :route_name, :password, :email, :background_image, :description, :hometown, :genre) #error here 
    end 

,這裏是我的錯誤信息

"Hello from update" 
"Logged in!" 
"=====================TASTY TACOS!" 
{"action"=>"update", "controller"=>"artists", "id"=>"1"} 
F 

Failures: 

    1) Current Artist editing contenteditables should text "Pizza" 
    Failure/Error: Unable to find matching line from backtrace 
    ActionController::ParameterMissing: 
     param is missing or the value is empty: artist 
    # ./app/controllers/artists_controller.rb:151:in `artist_params' 
    # ./app/controllers/artists_controller.rb:48:in `update' 

Finished in 15.41 seconds (files took 16.02 seconds to load) 
1 example, 1 failure 

Failed examples: 

rspec ./spec/features/editting_artist_spec.rb:32 # Current Artist editting contenteditables should text "Pizza" 

謝謝您的幫助。

+0

如果您使用'page.find('#similarArtists')。blur'代替了什麼?我不知道'.blur'和'之間是否有顯着差異。點擊',但這必須得到證實/駁斥 – MrYoshiji

+0

它沒有工作。我嘗試了'page.find('#similarArtists')。blur'和'page.find('#artistdescription')。blur',但都不起作用: '失敗/錯誤:page.find('#artistdescription')。模糊 NoMethodError:未定義的方法'模糊'的#<水豚::節點::元素:0xbaa60814>' – Finch

+0

嘿,我只是注意到你設置事件偵聽器'$(document).on('blur','#artistdescription ',handler)',但是你的模擬點擊動作在'page.find('#similarArtists')。click'上。也許它應該是'page.find('#artistdescription')。click'? – MrYoshiji

回答

2

原來這是與QtWebKit的本身有問題,如雀如上所述: https://github.com/thoughtbot/capybara-webkit/issues/553

有一種解決方法,在發送POST請求並手動重定向到update控制器方法(這裏詳細說明):
https://stackoverflow.com/a/17870287/472768

+0

它的工作,參數顯示的描述! '「你好,從更新」 「登錄!」 「===================== TASTY TACOS!」 { 「動作」=> 「更新」, 「藝術家」=> { 「描述」=> 「披薩」}, 「session_code」=> 「jeBPERlH2y2utkh28CjChw」, 「控制器」=> 「藝術家」, 「 id「=>」1「} {」description「=>」Pizza「} 」===================== TASTY TACOS!「 { 「動作」=> 「更新」, 「藝術家」=> { 「描述」=> 「披薩」}, 「session_code」=> 「jeBPERlH2y2utkh28CjChw」, 「控制器」=> 「藝術家」, 「 ID 「=>」 1" }' – Finch

1

解決此問題的一個好方法是強制頁面等待,直到所有未完成的ajax請求完成,然後再允許測試繼續。要點是每次請求完成時和每次請求完成時都必須記錄日誌。

在js文件called test.js

//= require jquery 

var requestsFinished = false; 

$(function() { 
    $(document).ajaxStop(function() { 
    requestsFinished = true; 
    }); 
}); 

在你.erb模板

<%= javascript_include_tag 'test' if Rails.env.test? %> 

spec_helper.rb

config.include(RequestHelper, :type => :feature) 

在一個名爲request_helper.rb

module RequestHelper 
    def self.included(base) 
    base.metadata[:js] = true 

    base.send(:include, InstanceMethods) 
    end 

    class InstanceMethods 
    def wait_for_ajax 
     page.wait_until { 
     page.execute_script(<<-JS) 
      if(!requestsFinished) { 
      throw "Requests not finished"; 
      } 
     JS 
     } 
    end 

最後,在你要求的測試

page.execute_script(<<-JS) 
    $(document).trigger('blur'); 
JS 

wait_for_ajax 

page.should have_content('Success') 
+0

我目前沒有提交按鈕。該功能旨在節省模糊。 – Finch

+0

直接在已設置事件處理程序的元素上觸發模糊。 (在你的案例文件中)'$(document).trigger('blur');' – nort