jeudi 2 juillet 2015

How to run minitest for controller methods?

post_controller file

class PostsController < ActionController::Base
before_action :authenticate_user!

def index
 @post = current_user.posts.paginate(page: params[:page], per_page: 5)
 respond_to do |format|
   format.html
   format.json { render json: @post }
 end
end

def new
 @post = Post.new
end

def create
 @post = current_user.posts.build(post_param)
 if @post.save
   redirect_to action: 'index'
 else
   render 'new'
 end

post_controller_test

require 'test_helper'

class PostsControllerTest < ActionController::TestCase
include Devise::TestHelpers

def setup
 @user  = users(:Bob)
 @post = Post.new
end  #passed

test 'logged in should get show' do
 sign_in @user
 get :index
 assert_response :success
end  #passed

test 'not authenticated should get redirect' do
 get :index
 assert_response :redirect
end   #passed  

test 'should get index' do
 get :index
 assert_response :success
 assert_not_nil assigns(:posts)
end   #failing

test "should destroy post" do
 assert_difference('Post.count', -1) do
 delete :destroy, id: @post
end

assert_redirected_to posts_path
end   #failing
...  

devise is setup and working fine but why I am getting 302 error in last two cases. Is it because I am not passing @user parameters to it? I did but it was still throwing the same error. I also checked out my routes file which is fine because post_controller is working fine in development mode.

What I am doing wrong here?

Aucun commentaire:

Enregistrer un commentaire