0
votes

It's a simple (and maybe common) question but I can't find a clear answer.

I'm testing my rails engine from the dummy application with Rspec and Capybara. In my feature spec I need to use a ApplicationController method named get_component() with a specific argument. How can I do it?

My feature test is:

require "rails_helper"

describe "My test", type: :feature do
    it "gets a component if slug is provided" do
        # ...
        component = ApplicationController.get_component( slug )
        # ...
    end
end

Which returns

NoMethodError:
undefined method `get_component' for #<ApplicationController:0x007ff7207234f8>

I don't want to mock/stub that method, I want it to run for real.

Also, the application ApplicationController should inherit the engine one, shouldn't it? So it's right to call ApplicationController, isn't it?

1
The reason you can't find any info on this is because you really shouldn't be doing it in a feature test. Feature tests should be from the users perspective through the browser.Thomas Walpole
I see, but I needed to check if what the user has achieved is what the feature is expected to be doing: i.e. adding a new component after having completed the form. Is this still something I shouldn't be doing?a.barbieri
Assuming the component is shown to the user somewhere in the app, you should be checking the component shows up in the UI rather than attempting to grab it directly form the DB. What you're attempting is more suited for a different type of test (request, controller, etc)Thomas Walpole
I see what you mean. A question though: in a controller test would it be written differently? Apparently using that method (which belongs to the engine) from the app will need that trick anyway. The ApplicationRecord which belongs to the app doesn't seem to inherit the method belonging to ApplicationRecord of the engine. Or maybe I'm doing something wrong, am I?a.barbieri
I'm not sure I understand your question or why you're now talking about ApplicationRecord? The main apps ApplicationController does not inherit from the engines, they are separately scoped - guides.rubyonrails.org/engines.html. Controller tests are run in the scope of the controller being tested, so if you're testing in a controller that creates engine records, you should be able to load those engines record, unless I'm completely misunderstanding what you're trying to do.Thomas Walpole

1 Answers

2
votes

Remember that get_component is an instance method, not a class method, so you can only call it on an ApplicationController object, not on the class itself. Try this instead...

component = ApplicationController.new.get_component( slug )