lundi 11 janvier 2016

MiniTest Authentication

I'm new to MiniTest. Most of the testing is easy to grasp as it is Ruby code and also has Rspec-style readability. I am, however, having trouble with authentications. As with any app, most controllers are hidden behind some sort of authentication, the most common being authenticate_user to ensure that a user is logged in.

How do I test a session -> user is logged in?

I do have this as a reference: http://ift.tt/1OezxYk

But not quite sure how to implement it.

Let's use this as an example controller:

class ProductsController < ApplicationController
  before_action :authenticate_user

  def index
    @products = Product.all
  end

  def show
   @product = Product.find(params[:id])
  end

end

How would my test look in these basic instances?

test "it should GET products index" do
  # insert code to check authenticate_user
  get :index
  assert_response :success
end

test "it should GET products show" do
  # insert code to check authenticate_user
  get :show
  assert_response :success
end

#refactor so logged in only has to be defined once across controllers.

Aucun commentaire:

Enregistrer un commentaire