0

I have a feature spec: /spec/features/posts_features_spec.rb:

require 'spec_helper'

describe "Displaying Posts" do
  it "can see posts on posts page" do
    Post.create(title: "Title", content: "Content")
    visit '/posts'
    expect(page).to have_content("Title")
    expect(page).to have_content("Content")
  end
end

When I run the test, I get:

WARNING! No apps are mounted. Please, mount apps in `config/apps.rb`

And a failing test, with:

NameError:
       uninitialized constant Post

/spec/spec_helper.rb:

require 'rack/test'
require 'rspec/padrino'
require 'capybara/rspec'

Capybara.app = Padrino.application

RSpec.configure do |config|
  config.include Rack::Test::Methods
  config.include RSpec::Padrino
  config.include Capybara::DSL

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

/Gemfile:

source 'https://rubygems.org'

# Padrino supports Ruby version 1.9 and later
ruby '2.3.0'

# Project requirements
gem 'rake'

# Component requirements
gem 'bcrypt'
gem 'sass'
gem 'haml'
gem 'activerecord', '>= 3.1', :require => 'active_record'
gem 'sqlite3'

# Test requirements
group :test do
  gem 'shoulda'
  gem 'rack-test', :require => 'rack/test'
  gem 'rspec-padrino'
  gem 'capybara'
end

# Padrino Stable Gem
gem 'padrino', '0.13.1'

/app/app.rb:

module PadrinoBlog
  class App < Padrino::Application
    register SassInitializer
    use ConnectionPoolManagement
    register Padrino::Mailer
    register Padrino::Helpers

    enable :sessions

    get "/" do
      "Hello World!"
    end

    get :about, :map => '/about_us' do
      render :haml, "%p This is a sample blog created to demonstrate how Padrino works!"
    end
  end
end

/config/apps.rb:

Padrino.configure_apps do
  # enable :sessions
  set :session_secret, '67c26da8db30eaf68907e2de05783d33be5e246b87c194f0579bdb589c0761dd'
  set :protection, :except => :path_traversal
  set :protect_from_csrf, true
end

# Mounts the core application for this project

Padrino.mount("PadrinoBlog::Admin", :app_file => Padrino.root('admin/app.rb')).to("/admin")
Padrino.mount('PadrinoBlog::App', :app_file => Padrino.root('app/app.rb')).to('/')

I've tried various different things, such as requiring the 'app.rb' file in spec_helper, which then leads to having to require active_record there too, which then leads to a problem because it doesn't know what SassInitializer is, and on and on.

To be honest, I don't really know what I'm doing with this or how everything fits together, so any help would be much appreciated.

The repo's branch here.

Yorkshireman
  • 2,194
  • 1
  • 21
  • 36

1 Answers1

1

I've never used padrino, but it appears you're missing a few lines from the top of your spec_helper that would actually load up the app resources

RACK_ENV = 'test' unless defined?(RACK_ENV)
require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
Dir[File.expand_path(File.dirname(__FILE__) + "/../app/helpers/**/*.rb")].each(&method(:require))
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • I've copy/pasted these things in. It's moved the error message on - it is now 'Failure/Error: Post.create(title: "Title", content: "Content")' ActiveRecord::StatementInvalid: Could not find table 'posts' – Yorkshireman Jan 26 '16 at 20:03
  • Sounds like you need to setup/migrate your database in the test environment – Thomas Walpole Jan 26 '16 at 20:04
  • "padrino rake ar:migrate -e test" fixed this - it seems that in Padrino rake db:migrate just runs migrations for the development environment. – Yorkshireman Jan 26 '16 at 20:10
  • It's working now, which is great! However, could someone PLEEEASE explain what the lines MEAN? I hate copy/pasting code without understanding what's going on. – Yorkshireman Jan 26 '16 at 20:17
  • first line just sets the default environment to test, second line loads the config/boot.rb file which in turn loads Padrino and the app - the third line loads all the helpers, which may not be needed but its put there by padrino if you create a new app while telling it to use rspec for testing (-t rspec) – Thomas Walpole Jan 26 '16 at 20:22
  • Tom, instead of the Rack::Builder etc it seems to work fine with `Capybara.app = Padrino.application`. Can you see anything wrong with this? – Yorkshireman Jan 26 '16 at 21:09
  • Ok - as I said I wasn't sure if it was needed -- I just didn't know what exactly the Padrino.application call returned -- sounds like it returns a rack app - so it's probably fine - I'll remove that part from my answer – Thomas Walpole Jan 26 '16 at 21:10