ruby-on-railsrspecdevisealchemy-cms

Ruby on Rails - Rspec test with Devise is failing


I have a Problem with an rspec-request-test in my rails app. The tests, that need a user-login are failing. I have implemented the devise-tests according to this tutorial. Also I found many links, that are using a similar approach. e.g. here

Here is an extract of my gemfile:

gem 'devise', '~> 3.5'
gem 'devise-i18n'
gem 'devise-i18n-views'
gem 'cancancan', '~> 1.10'
gem 'rolify'
gem 'alchemy_cms', '~> 3.2.0'
gem 'rspec-core'
gem 'rspec-rails'
gem 'i18n-tasks', '~> 0.8.7'
gem 'factory_girl_rails'

Here is the rspec-test, that is failing: (spec/requests/campaigns_spec.rb)

require 'rails_helper'
RSpec.describe "Campaigns", type: :request do
  describe "GET /campaigns" do
    it "responds with 200" do
      sign_in_as_a_valid_user_request
      get campaigns_path
      expect(response).to have_http_status(200)
    end
  end
end

You should only see this page, when you are logged in. So in the test I want to log in as a User (created with FactoryGirl), than go to the site, and get a 200-response. In the application itself this is working good.

spec/rails_helper.rb (extract)

...
require 'spec_helper'
require 'rspec/rails'
require 'devise'
require 'support/devise_support'
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.include Devise::TestHelpers, type: :controller
  config.include Devise::TestHelpers, type: :view
  config.include ValidUserRequestHelper

  ...
end

spec/support/devise_support.rb

module ValidUserRequestHelper
  def sign_in_as_a_valid_user_request
    @user ||= FactoryGirl.create :user
    post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
  end
end

here is the error, that the test is throwing:

1) Campaigns GET /campaigns responds with 200
   Failure/Error: sign_in_as_a_valid_user_request
   ActionController::RoutingError:
     Alchemy::Page not found "/"
   # ./spec/support/devise_support.rb:13:in `sign_in_as_a_valid_user_request'
   # ./spec/requests/campaigns_spec.rb:6:in `block (3 levels) in <top (required)>'

According to the links that I found, this should work! but it doesn't... Can anyone tell me why? Why is there a routing error? The app itself is working fine.


Solution

  • The routing error occurs, because your host app does not know of the engines routes. That's why you have to use the routing proxy objects of rails engines in views outside the current controller scope. Please read more about rails engines and the characteristics of their routes in the official rails guides

    Luckily, Alchemy comes with a handy test helper that solves your problems. Just add this to your spec_helper:

    # spec/spec_helper.rb
    require 'alchemy/test_support/controller_requests'
    ...
    RSpec.configure do |config|
      config.include Alchemy::TestSupport::ControllerRequests
      ...
    end
    

    Know you can use alchemy_get instead of get in your request specs.