1

I am developing a Rails 4.1 application that sits alongside Alchemy CMS. I do not want the admin portion to use Alchemy's admin interface. (I am building my own with ZURB Foundation.)

Trouble starts brewing when I want to run my own specs in RSpec. While the application runs fine in development mode, I get this error in my tests:

Failure/Error: visit new_staff_program_path
     Alchemy::DefaultLanguageNotFoundError:
       No default language found. Have you run the rake alchemy:db:seed task?

It appears that the controller is running some filters that expect data to be in place. Is there a way that I can "opt out" of this for certain parts of my application?

For now, this is my solution...

I have a base controller that the separate admin is extending. Within that, I instruct it to skip before_action calls that the Alchemy engine adds to the controller layer:

class Staff::Base < ApplicationController
  # Filters
  skip_before_action :set_current_alchemy_site, :set_alchemy_language

  # Layout
  layout 'admin'
end

If anyone has any better solutions, let me know.

Chris Peters
  • 17,918
  • 6
  • 49
  • 65

2 Answers2

5

Yes, it is indeed very simple ;)

In your spec_helper you can add a Rspec.config block that will seed the base data of Alchemy everytime you run your test suite:

# spec/spec_helper.rb
require 'alchemy/seeder'

RSpec.configure do |config|
  config.before type: :feature do
    Alchemy::Seeder.seed!
  end
end
Chris Peters
  • 17,918
  • 6
  • 49
  • 65
tvdeyen
  • 723
  • 4
  • 9
  • I get this error: `uninitialized constant Alchemy::Seeder (NameError)`. Is there something that I need to `require` to get that module/class included? – Chris Peters Oct 23 '14 at 12:15
  • Ah, yes. Sorry. You need to add `require 'alchemy/seeder'` into your `spec_helper.rb` – tvdeyen Oct 23 '14 at 12:16
  • 1
    I am using `database_cleaner`, so I changed it to `before(:each)`, and it works like a charm. Also, I placed that code into `spec/support/alchemy_cms.rb`. I'm guessing that is more of a point of preference. Thank you! – Chris Peters Oct 23 '14 at 12:16
  • OK, I guess I only need it in specs where the controller is loaded. So I edited your answer to include `type: :feature`. I'd assume that I'll need to change it to include `request` and `controller` specs if I decide to write any of those. I am well pleased with this, so thanks again. – Chris Peters Oct 23 '14 at 12:34
  • 1
    For a fully featured use of `DatabaseCleaner` and `Alchemy::Seeder` please refer the `spec_helper.rb` of Alchemy's very own test suite: https://github.com/magiclabs/alchemy_cms/blob/master/spec/spec_helper.rb#L63-L88 – tvdeyen Oct 23 '14 at 12:36
0

And the brute-force way:

# 'spec/support/alchemy_stub'    
# Make tests run faster by stubbing Alchemy controller before actions

module Alchemy
  module ControllerActions
    def set_current_alchemy_site
    end
    def set_alchemy_language
    end
  end
end

Require it in your spec_helper if you don't auto-require everything in spec/support

# spec/spec_helper.rb
require 'support/alchemy_stub'
Martin Tomov
  • 31
  • 1
  • 5
  • You definitely don't want this in your integration/feature specs. So remember to only load this patch in your controller unit tests. – tvdeyen Dec 30 '14 at 13:34