percy / percy-capybara

Visual testing for Capybara with Percy.
https://docs.percy.io/docs/capybara
MIT License
45 stars 23 forks source link

Percy errors if being run from a Rails repo that does not have assets #18

Closed bewatts closed 7 years ago

bewatts commented 7 years ago

I'm trying out Percy, and getting the following:

NoMethodError:
  undefined method `assets' for #<Rails::Application::Configuration:0x007fb11f4c51f8>
  Did you mean?  asset_host

It looks like it's coming from:

        if defined?(Rails)
          @sprockets_environment = options[:sprockets_environment] || Rails.application.assets
          @sprockets_options = options[:sprockets_options] || Rails.application.config.assets
        end

Admittedly, we've got an oddball setup right now:

  1. We're using a Rails API and serving no assets from it
  2. We've got a react app embedded in the repo
  3. We're using capybara (in our integration specs) from the react app.

Which leaves us in the case where Rails is defined, we are using capybara, but we are do not have any Rails assets.

The following fixes it for me, simply doing a nil check should suffice.

Rails.application.config.send(:define_singleton_method, :assets) { nil } unless Rails.application.config.respond_to?(:assets)
timhaines commented 7 years ago

Hi @bewatts, we talked a little about this off-github - and I think you got things working correctly by switching to our NativeLoader and by adjusting gulp to compile the CSS to disk.

We've since released an update to the percy-capybara gem, that adds a FilesystemLoader. If you do have your css and images etc being compiled into a folder as part of your build process, then I'd recommend switching to the FilesystemLoader (it's faster, and solves a few other problems related to asset identification).

You configure it like this, where you're currently initializing your build:

assets_dir = File.expand_path("../../dist_assets", __FILE__)
Percy::Capybara.use_loader(:filesystem, assets_dir: assets_dir, base_url: '/assets')
Percy::Capybara.initialize_build

You will also need to specify an assets_dir. assets_dir is the absolute path where Percy can find your assets, and should contain your compiled static assets rather than your source assets.

You can also optionally provide a base_url if your web-server serves assets with a path prefix.

If you try this, let me know how you get on?

Also, I'd love to understand a little more about your setup so we can better cater to this in the future. How are you embedding react in rails / serving html? Are you using react-rails or something similar?

Thanks Ben!