6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails EngineでCapybaraを導入しようとしてハマったこと

Last updated at Posted at 2016-12-25

Rails EngineにCapybaraを導入しようとしてハマったことが二つあるので、備忘録として残しておきたいと思います。

ルーティングを認識させる

Railsアプリを普通に書いている場合は気にする必要がないのですが、Rails Engineでfeature specをいきなり動かそうとすると、visit (path)のところで以下のようなエラーが発生します。

  1) method_call_situation pages (permit WebUI) render Succsess
     Failure/Error: visit method_call_situations_path
     
     NameError:
       undefined local variable or method `method_call_situations_path' for #<RSpec::ExampleGroups::MethodCallSituationPages::PermitWebUI:0x007fe1d21b1730>
     # ./spec/features/method_call_situations_page_spec.rb:34:in `block (2 levels) in <top (required)>'

Rails Engineではspec内でルーティングを認識させる必要があります。

spec_helper.rb
...
RSpec.configure do |config|
  config.include OkuribitoRails::Engine.routes.url_helpers
end
...

これで、名前付きルートが認識されるようになります。

asset precompile を行うようにする

feature spec内でRails Engine内で持っている画像等のAssetを参照しようとすると、以下のようなエラーが発生します。

1) method_call_situation pages (permit WebUI) available search function
     Failure/Error: <%= image_tag "okuribito_rails/logo.png", class: "header-logo" %>
     
     ActionView::Template::Error:
       Asset was not declared to be precompiled in production.
       Add `Rails.application.config.assets.precompile += %w( okuribito_rails/logo.png )` to `config/initializers/assets.rb` and restart your server
     # ./app/views/layouts/okuribito_rails/_header.html.erb:2:in `___sers_ym_works_okuribito_rails_dev_app_views_layouts_okuribito_rails__header_html_erb__3728936773133030127_70218317871640'
     # ./app/views/layouts/okuribito_rails/application.html.erb:11:in `___sers_ym_works_okuribito_rails_dev_app_views_layouts_okuribito_rails_application_html_erb___1363027026573178022_70218307163340'
     # ./spec/features/method_call_situations_page_spec.rb:34:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # Sprockets::Rails::Helper::AssetNotPrecompiled:
     #   Asset was not declared to be precompiled in production.
     #   Add `Rails.application.config.assets.precompile += %w( okuribito_rails/logo.png )` to `config/initializers/assets.rb` and restart your server
     #   ./app/views/layouts/okuribito_rails/_header.html.erb:2:in `___sers_ym_works_okuribito_rails_dev_app_views_layouts_okuribito_rails__header_html_erb__3728936773133030127_70218317871640'

Rails Engine単体では、asset precompileを行うように書かれていないハズなので、当たり前っちゃ当たり前です。
そこで、以下のようにengine.rbにて、asset precompileを行う設定にします。

engine.rb
module OkuribitoRails
  class Engine < ::Rails::Engine
    isolate_namespace OkuribitoRails

    initializer "okuribito_rails.assets.precompile", group: :all do |app|
      app.config.assets.precompile += %w(
        okuribito_rails/logo.png
      )
    end
  end
end

これで、テストが通るようになります。

なお、この対処方法が正しいかどうかは残念ながら保証しかねますが、rails_adminというgemのやり方を参考にしています。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?