mercredi 19 septembre 2018

Rails 5 integration test and current_user

I am writing an integration test for my Rails 5 application. One of my tests is interested in seeing if a particular piece of HTML shows up after a URL is visited.

This part of the DOM is only embedded into the document if the current_user object is set and is set to the owner of the fixture I created.

Let me show some code:

require 'test_helper'

class HomeControllerTest < ActionDispatch::IntegrationTest
    fixtures :homes, :users

    setup do
        @user = users(:one) 
        @home  = homes(:one)
    end

    test 'home show should show title' do
        get home_path(@home)

        assert_select "h3", @home.title
    end

    test 'user should sign in and see a Welcome to your home header' do

        get auth_login_path(user: @user)        

        assert_redirected_to user_path(@user)

        get home_path(@home)

        assert_select "h3", "Welcome to your home"
    end
end

The problem with this test is current_user is never set as far as I can tell OR current_user does not equal to @user. I'm not entirely sure what other setup is required to get those two objects to equal each other so I can test parts of the user interface that are specific to a user being logged in and visiting their own home.

Aucun commentaire:

Enregistrer un commentaire