lundi 22 mai 2017

Which is the most appropriate place to include the helper file to make its methods available in integration test?

I'm trying to write a test for checking the site layout. In the test I'm trying to access the current_user method defined in the sessions_helper file.

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end

  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", contact_path
    get login_path
    post login_path, params: { session: { email: @user.email,
                                       password: 'password' }}
    get root_path
    assert_template 'static_pages/home'
    assert_not current_user.nil?
    assert_select "a[href=?]", signup_path, count: 0
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", signup_path
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path, count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
  end
end

Currently, running the test results in the following error:-

NameError:         NameError: undefined local variable or method `current_user' for #<SiteLayoutTest:0x00000006841600>
            test/integration/site_layout_test.rb:19:in `block in <class:SiteLayoutTest>'

I have included the sessions_helper in the application_controller.rb, as follows:-

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end

I thought adding it here would make it available in the tests automatically. But apparently not.

I'm a rails newbie and am still struggling to wrap my head around the structure of the various modules/classes.

Which is the best place to include the sessions_helper file to make its method available for the integration test?

Also, is there a resource that you could recommend that succinctly illustrates the structure/hierarchy of the various classes in the rails framework? I want to get a sense of how they are all related.

Thanks.

Aucun commentaire:

Enregistrer un commentaire