samedi 15 octobre 2016

How to test a custom controller action

In my application home page, users can make a choice from a list using a form.
The form collects the choice and sends it to a controller custom action whose only goal is to update the user's attribute choice:

<% form_tag({controller: "weekly_selections", action: "update_choice"}) do %>
    <div><%= hidden_field_tag :user_choice, book.title %></div>
    <%= submit_tag("choose", class: "btn btn-primary btn-sm") %>
<% end %>

I created a WeeklySelection controller and a custom update_choice action:

class WeeklySelectionsController < ApplicationController

  def update_choice
    if current_user.choice_count < 10
      user_choice = params[:user_choice]
      current_user.update_attribute(:choice, user_choice)
      current_user.increment!(:choice_count)
      flash[:success] = 'Your choice has been recorded'
      redirect_to root_url
    else
      flash[:warning] = "Attention! You already made 10 choices"
      redirect_to root_url
    end
  end

end

How can I test the correct behaviour of this action?

Aucun commentaire:

Enregistrer un commentaire