'Uninitialized constant error after moving page models

I am using Selenium, Capybara, and Rspec to write UI tests. Our project has lots of helpers and page models. I would like to sort out the helpers and page models that are only used in specific test groupings.

This is a sample of what our current project structure is like:

spec
- test grouping folder 1
    - test 1
    - test 2
- test grouping folder 2
    - another test
    - another test
- helpers
    - helper 1
    - helper 2
- page_models
    - page 1
    - page 2
- spec_helper.rb

This is what I am trying to implement:

spec
- test grouping folder 1
    - helpers
        - helper file 1
    - page_models
        - page model 1
        - page model 2
    - test 1 (spec file)
    - test 2 (spec file)
- helpers (shared between multiple test groupings)
- page_models (shared between multiple test groupings)
- spec_helper.rb

I started by moving the relavent helper and page model files into test group 1's new helpers and page_models folders. Then in my spec helper I added the following:

require "./spec/test_group_1/helpers/helper_1"
Dir[File.dirname(__FILE__) + "spec/**/helpers/*.rb"].each { |f| require f }
Dir[File.dirname(__FILE__) + "spec/**/page_models/*.rb"].each { |f| require f }

When I run my tests I get this error:

Failure/Error: subject(:page) { PageModel1.new }
     NameError:
       uninitialized constant PageModel1

this is what the test looks like:

describe "TestGroup1", type: :feature do
  describe "League CRUD" do
    context "with an authorized user" do
      subject(:page) { PageModel1.new }
      
      before do
        visit "my url here"
        page.sign_in.click
        login
      end

      it "creates a league" do
        add_new_sb_page
        create_league
        expect(page).to have_content("League 1")
      end
    end
  end
end

This is what my page_model looks like:

class PageModel1 < SitePrism::Page
  element :example, "div#example"
end

Why would changing the structure cause this error? Is there a better way to structure my project?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source