Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec check the template after a redirect

Here is my create action for users:

def create
        @user = User.new(user_params)

        if @user.save
            respond_to do |format|
                format.html { 
                    redirect_to edit_admin_user_path(@user) 
                    flash[:success] = "Successfully created"
                }
            end
        else
            render :new
            flash[:alert] = "Something went wrong"
        end
    end

My test is looking like this:

context "POST methods" do

    describe "#create" do
        it "renders the edit template" do
            post :create, user: FactoryGirl.attributes_for(:user)
            expect(response).to render_template(:edit)
        end
    end
end

However I'm getting this error:

Failures:

  1) Admin::UsersController POST methods #create renders the edit template
     Failure/Error: expect(response).to render_template(:edit)
       expecting <"edit"> but was a redirect to <http://test.host/admin/users/80/edit>

I want to check if the edit.html.haml file is rendered after creating a user. What am I doing wrong?

Update #1 I do check for redirect in another test, this is my full test suite:

require 'rails_helper'

RSpec.describe Admin::UsersController, type: :controller do

    render_views    

    context "POST methods" do

        describe "#create" do
            it "using valid params" do

                expect{
                    post :create, user: { email: "[email protected]", password: "long12345678" }
                    }.to change(User, :count).by(1)
                # get user_path('1')
            end

            it "redirects to the edit page after saving" do
                post :create, user: FactoryGirl.attributes_for(:user)
                user = User.last
                expect(response).to redirect_to(edit_admin_user_path(user.id))
            end

            it "renders the edit template" do
                post :create, user: FactoryGirl.attributes_for(:user)
                user = User.last
                expect {
                    redirect_to(edit_admin_user_path(user.id))  
                }.to render_template(:edit)
            end

            context "it redirects to new" do
                it "if user has no valid mail" do
                    post :create, user: { email: "something", password: "long12345678" }
                    expect(response).to render_template(:new)
                end
                it "if user has no valid password" do
                    post :create, user: { email: "[email protected]", password: "short" }  
                    expect(response).to render_template(:new)
                end
            end
        end
    end
end

What I want is to actually check if the edit template is rendered. Because with expect(response).to redirect_to(edit_admin_user_path(user)) it does not check the template. This test passes even if I have no edit.html.haml file at all.

like image 913
mohnstrudel Avatar asked Oct 18 '22 17:10

mohnstrudel


1 Answers

When you're testing create action you should just check correctness of redirect. In this action you're not actually rendering edit template, but you're just making redirect to the edit path of created entity. So this is the thing you should check.

describe "#create" do
    it "redirects to the edit path" do
        post :create, user: FactoryGirl.attributes_for(:user)
        expect(response).to redirect_to(edit_admin_user_path(User.last))
    end
end

Then you should have another test for edit action, where you're checking template rendering. That will mean that after redirect in create action you also will see the proper template.

like image 194
ktaras Avatar answered Oct 21 '22 06:10

ktaras