2017-01-05 58 views
0
describe 'Destroy Student Record' do 
     it 'should allow to Delete My student', js: true do 
      expect { 
      find('.icon-delete',"a[href ='/room/grade3/#{@student.id}']").click 
      page.find('.btn.delete', text: 'Sure').click 
      }.to change(Student, :count).by(-1) 
     end 
     end 

但問題是,當他們的2個記錄就說明.icon,刪除是不明確的,也href是相同的刪除和查看詳細信息,以便爲HREF也它顯示不明確。 我怎樣才能做一個組合搜索如何做一個組合搜索點擊墨Cypabara Rspec的

+0

找不到2個字符串 - 顯示您要點擊的實際HTML –

+0

jerrytom

回答

2

隨着HTML

<a href="/room/grade3/10036" class="btn" title="Delete"> 
    <i class="icon-delete"></i> 
</a> 

有多種方法點擊 - click_link將匹配上的標識,文本或鏈接的標題所以只需點擊鏈接你可以做

click_link('Delete', href: "/room/grade3/#{@student.id}") 

如果您需要在<明確點擊我>元素,你可以做任何的

find_link('Delete', href: "/room/grade3/#{@student.id}").find('.icon-delete').click 
find("a[href ='/room/grade3/#{@student.id}'] i.icon-delete").click 

注意:即使有這些修復程序,您的測試可能會失敗。這是因爲你使用的是匹配器,並且你的塊沒有做任何事情來確保在更改匹配器再次檢查學生計數之前動作已經完成。這是因爲,當使用任何JS支持驅動程序click時,只需單擊該按鈕,它不知道任何等待的副作用,並在應用程序關閉時返回測試,並開始處理單擊觸發的任何行爲。爲了解決您的測試將需要像

expect { 
    ... Do whatever clicking of link/button ... 
    expect(page).to have_text("Student deleted") # whatever message is shown once the student is actually deleted or an assertion for other visible change 
}.to change(Student, :count).by(-1) 

這樣的代碼將延遲,直到學生實際刪除,change匹配可以得到學生重新計數。