Jump to content

The ultimate community for Ruby on Rails developers.


how would I test main_app routes in rails engine tests with rspec?

rspec engine

  • Please log in to reply
No replies to this topic

#1 Vell



  • Members
  • 100 posts
  • LocationWashington, DC

Posted 02 August 2014 - 04:26 AM

I am writing a basic authentication engine and I am at the point where I am writing controller tests for this engine.  I am writing the session controller test where I am redirecting to the main_app.root_path after the login was successful.


Is it possible to use main_app.root_path in my tests? or is the only way to test this is in the dummy app?


For instance, if I have the following in in sessions#create

def create
      user = Authenticatable::User.find_by(email: params[:email])
      if user && user.login(params[:password])
        session[:user_id] = user.id
        redirect_to main_app.root_path, notice: "You are signed in!"
        flash[:error] = 'Invalid email or password'
        redirect_to sign_in_path

and in my spec test I have

 describe "GET create" do
      context "successfull session creation" do
        let(:user) {FactoryGirl.create :user}

      it 'sets user id session' do
       get :create, use_route: :authenticatable, email: user.email, password: 'password'
          expect(session[:user_id]).to eq user.id
The test would pass if not for the main_app.root redirect that is happening in the controller. I get the following error:
undefined method `root_path' for #<ActionDispatch::Routing::RoutesProxy:0x007fb2da3d3530>

Does it make sense to just write all of my rspec tests in the dummy app so that I have access to all the routes? I guess cause I can write tests in both places, I'm curious where the best place is to write which tests.


Also tagged with one or more of these keywords: rspec, engine

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users