Question: Ajax Datatables testing with rspec and capybara

Question

Ajax Datatables testing with rspec and capybara

Answers 0
Added at 2017-07-25 15:07
Tags
Question

I'm trying to create tests for my app, but I can't make my tests for pages with datatables to work. I'm loading the records to the table with ajax. If I go to the browser everything works fine but none of the tests pass.

Heres my code.

index.html.slim

table.table.table-striped.table-bordered.table-hover.table-checkable.order-column#dtable data-source="#{ admin_analysts_path(format: :json)}"
  thead
    tr
      th Nome
      th email
  tbody

analysts_controller def index add_breadcrumb "Todos Analistas"

respond_to do |format|
  format.html
  format.json { render json: AnalystsDatatable.new(view_context)}
end

end

analysts_datatable.rb

class AnalystsDatatable
  delegate :params, :h, :link_to, :number_to_currency, to: :@view

  def initialize(view)
    @view = view
  end

  def as_json(options = {})
    {
      draw: params[:draw].to_i,
      recordsTotal: Analyst.count,
      recordsFiltered: analysts.total_entries,
      data: data
    }
  end

private

  def data
    analysts.map do |analyst|
      [
        link_to(analyst.name, [:admin,analyst]),
        (analyst.email)
      ]
    end
  end

  def analysts
    @analysts ||= fetch_analysts
  end

  def fetch_analysts
    analysts = Analyst.order("#{sort_column} #{sort_direction}")
    analysts = analysts.page(page)
    if params[:length]
      analysts = analysts.per_page(params[:length])
    end
    if params[:search][:value].present?
      analysts = analysts.where("name like :search or email like :search", search: "%#{params[:search][:value]}%")
    end
    analysts
  end

  def page
    params[:start].to_i == 0 ? 1 : ((params[:start].to_i)/params[:length].to_i) +1
  end

  def sort_column
    columns = %w[name email]
    columns[params[:order]['0'][:column].to_i]

  end

  def sort_direction
    params[:order]['0'][:dir] == "desc" ? "desc" : "asc"
  end
end

And my test configuration for javascript spec_helper.rb

Capybara.javascript_driver = :webkit

and finally my test

scenario 'check all analysts', :js => true  do

    login_as administrator, scope: :administrator

    analyst1
    visit admin_analysts_path

    expect(page).to have_content("#{analyst.name}")
    expect(page).to have_content("#{analyst1.name}")
  end

I saw one answer that sugested to change the capybara server because the get uri was to large, I changed to puma but it didn't work. Also tried to use find method that is said to wait till the element is loaded.

Any suggestion is valid, thanks.

Answers to

Ajax Datatables testing with rspec and capybara

Source Show
◀ Wstecz