1
votes

Currently doing Rails Unit Test, using minitest-rails.

I'm using bootstrap editable js to directly update data in view.

I having trouble asserting value correctly, got Failure result.

Only for the function that I used bootstrap editable, since it uses other way to send parameters than normal Rails update action.

Please help a look at my codes.

In my controller:

def edit_job_type
  update_common_table('job_types', params[:pk], params[:name], params[:value])
end

In my included module:

def update_common_table(table, id, key, value)
  begin
    case table
    when 'job_types'
      @record = JobType.find(id)
    end

    case key
    when 'en_name'
      @record.en_name = params[:value]
      edit_field = 'English Name'
    end

    @record.last_updated_by = session[:username]
    @record.save

    render json: {
        status: 'success', 
        message: "#{edit_field} was successfully updated.", 
        updated_at: @record.updated_at.to_time.strftime("%a, %e %b %Y %H:%M"), 
        updated_by: session[:username]
    }
  rescue => error
    render json: {status: 'error', message: error.message}
  end
end

In my minitest-rails, controller:

setup do
  @job_type = job_types(:waiter)
end

test "should update job_type" do
  patch :edit_job_type, id: @job_type.id, job_type: { pk: @job_type.id, name: 'en_name', value: "janitor" }
  assert_response :success, message: 'English Name was successfully updated.'
  @job_type.reload

  assert_equal "janitor", @job_type.en_name # this one FAILS, not updated value
end

In my fixtures > job_types:

waiter:
 en_name: waiter

When I run rake test:

I got failure result, because the update was failed.

  Expected: "New Job Type Updated"
  Actual: "waiter"

Still getting the default value "waiter", instead of "janitor"

Please help to figure out how can I fixed my test.

1

1 Answers

0
votes

SOLVED

Finally I've made a work around after thorough searching.

The solution was to use a XHR method since bootstrap editable uses POST method.

Before:

test "should update job_type" do
  patch :edit_job_type, id: @job_type.id, job_type: { pk: @job_type.id, name: 'en_name', value: "janitor" }
  assert_response :success, message: ' Successfully updated.'
  @job_type.reload
  assert_equal "janitor", @job_type.en_name # this one FAILS, not updated value
end

After:

test "should update job_type" do
  xhr :post, :edit_job_type, format: :js, pk: @job_type.id, name: 'en_name', value: "janitor"
  assert_response :success, ' Successfully updated.'
  @job_type.reload
  assert_equal "janitor", @job_type.en_name
end

Thanks to this tutorial, Building Rails Test