W
W
weare1382014-12-25 15:47:45
Ruby on Rails
weare138, 2014-12-25 15:47:45

Rspec.How to pass admin to edit action?

Hello!
There are two controllers posts and admin_posts (accordingly, create, update, destroy actions are prohibited in the posts controller - only the admin from the admin_posts controller can create).
That is, if I do this

describe 'GET /edit_post' do
    it 'works for edit post' do
      get edit_post_path(post)
      expect(response).to have_http_status(200)
    end
  end

That test does not pass, which in principle should be. But when I want to test a logged in user
describe 'GET /edit_post' do
    it 'works for edit post' do
      user = FactoryGirl.create(:user, role: 'admin')
      post = FactoryGirl.create(:post)
      get edit_post_path(post)
      expect(response).to have_http_status(200)
    end
  end

then the shell renders an error
Failures:

  1) Posts GET /edit_post works for edit post
     Failure/Error: get edit_post_path(post)
     ActionView::Template::Error:
       First argument in form cannot contain nil or be empty

how to write the right test?
here is the posts controller itself
class PostsController < ApplicationController
  before_action :set_post, only: [:show]
  respond_to :html

  def index
    @posts = Post.page(params[:page]).per(3)
    respond_with(@posts)
  end

  def show
    @comments = @post.comments.page(params[:comments_page]).per(3)
    respond_with(@post)
  end

  def new
    @post = Post.new
    respond_with(@post)
  end

  def edit
  end

  def create
    @post = Post.new(post_params)
    @post.save
    respond_with(@post)
  end

  def update
    @post.update(post_params)
    respond_with(@post)
  end

  def destroy
    @post.destroy
    respond_with(@post)
  end

  private
    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:title, :body, :photo)
    end
end

I use devise and cancan gems

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2014-12-25
@weare138

Need to login user after creation

user = FactoryGirl.create(:user, role: 'admin')
sign_in user

And don't forget to include Devise's rspec helpers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question