vendredi 2 octobre 2015

Rails controller tests for user and admin

I want to test my controller for normal signed in user and for a user who is admin.

I wrote tests, and setup user, but when I have to setup admin and run tests for admin, I dont know how to do this

setup do
    user = User.create!(email: "example@mail.com", 
     first_name: "name", last_name: "surename", 
     password: "password", password_confirmation: "password")
    @post = user.posts.create!(title: "my title", content: "bla bla bla")
    sign_in user
end

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

test "should show post" do
    get :show, id: @post
    assert_response :success
end

test "should get #new" do
    get :new
    assert_response :redirect
end

test "should get #edit" do
    get :edit, id: @post
    assert_response :redirect
end

test "should get #destroy" do
    get :destroy, id: @post
    assert_response :redirect
end

=begin
test "should create post" do
    assert_difference('Post.count') do
        post :create, post: { title: "title", content: "content" }
    end

    assert_redirected_to post_path(assigns(:post))
end

test "should get #update" do
    patch :update, id: @post, post: { title: "oneone", content: "twotwo" }
    assert_redirected_to post_path(assigns(:post))
end

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

I want to test code between begin and end for admin, but how setup the admin user? I tried to do like this but it doesn't work.

setup do
 admin = User.create!(email: "example@mail.com", 
     first_name: "name", last_name: "surename", 
     password: "password", password_confirmation: "password", admin:true)
 sign_in admin
end

Aucun commentaire:

Enregistrer un commentaire