Given a Rails engine_one that has a spec support file engine_one/spec/support/system/order_functions.rb, containing functionality to support the testing of various order system tests such as simulating a logged in user, adding products to an order etc and contains methods such as log_visitor_in that get used extensively when testing order processing etc...
So now in engine_two that extends some ordering functionality from engine_one I wish to add a new system test that first has to log a visitor in. So how can I make use of that support method from from engine_one?
So far I have mounted the engines in the dummy app I have required engine_one in engine_two/lib/engine.rb I have required the support file in the relevant test but it can't be found and obviously I have added engine_one to engine_two.gemspec
engine_two/spec/rails_helper.rb
require 'engine_one' # and any other gems you need
engine_two/lib/engine_two/engine.rb
require 'engine_one'
in the relevant system test I have the following
engine_two/spec/system/new_payment_methods_spec.rb
require 'rails_helper'
include EngineOne::System
RSpec.describe "order_payment_feature", type: :system do
before do
driven_by(:rack_test)
end
it "has order payment options" do
log_visitor_in
end
end
This results in the following error
Failure/Error: include EngineOne::System
NameError:
uninitialized constant EngineOne::System
Did you mean? SystemExit
And the helper
module System
def log_visitor_in()
administrator = create(:visitor)
visit ccs_cms.login_url
fill_in 'login_name', with: visitor.login_name
fill_in 'Password', with: visitor.password
click_button 'Login'
end
end
I have tried with a require instead of an include but that results in a file not found error Plus I have tried changing the include path to
include EngineOne::Spec::Support::System resulting in the same error
So I guess I'm looking for the correct path but I am stuck or missing some other way to include the helper. These are Rails 7 engines.
When you
requirea file, ruby searches for it relative to paths in$LOAD_PATH; spec/ or test/ are not part of it.appdirectory is a special one in rails, any subdirectory automatically becomes part ofautoload_paths. Auto load paths can be seen hereActiveSupport::Dependencies.autoload_paths.Any classes/modules defined inside
app/*directories can be used without requiring corresponding files. Rails v7 useszeitwerkto automatically load/reload files by relying on the 'file name' to 'constant name' relationship. That's why folders map to namespaces and files map to classes/modules.To fix your issue put any shared code where it can be grabbed with
require. Type$LOAD_PATHin the console:Put shared files in engines's lib directory. Since we're outside of
appdirectory, rails is not the boss anymore, any path and filename combination will work.Now that file can be required
That's it, use it in your spec
Additionally you can require your files any way you wish with an absolute path, regardless of
$LOAD_PATH.