我想測試是否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"
謝謝您的幫助。
如果您使用'page.find('#similarArtists')。blur'代替了什麼?我不知道'.blur'和'之間是否有顯着差異。點擊',但這必須得到證實/駁斥 – MrYoshiji
它沒有工作。我嘗試了'page.find('#similarArtists')。blur'和'page.find('#artistdescription')。blur',但都不起作用: '失敗/錯誤:page.find('#artistdescription')。模糊 NoMethodError:未定義的方法'模糊'的#<水豚::節點::元素:0xbaa60814>' – Finch
嘿,我只是注意到你設置事件偵聽器'$(document).on('blur','#artistdescription ',handler)',但是你的模擬點擊動作在'page.find('#similarArtists')。click'上。也許它應該是'page.find('#artistdescription')。click'? – MrYoshiji