bploetz / versionist

A plugin for versioning Rails based RESTful APIs.
MIT License
972 stars 51 forks source link

RSpec undefined constant problem #66

Closed Atamanov closed 10 years ago

Atamanov commented 10 years ago

Hi there. I've got some problem with versionist and Rspec in my api.

My Gemfile.lock

GIT
  remote: git://github.com/bploetz/versionist.git
  revision: 5d12307fcd3ac72e6a23722253624a8e8c42806f
  specs:
    versionist (1.3.0)
      rails (>= 3)
      yard (~> 0.7)

GIT
  remote: git://github.com/mongoid/mongoid.git
  revision: 0e93563304484c7c0499e1fce5a2c08e96bd8584
  specs:
    mongoid (4.0.0)
      activemodel (~> 4.0)
      moped (~> 2.0.0)
      origin (~> 2.1)
      tzinfo (>= 0.3.37)

GIT
  remote: https://github.com/rails-api/rails-api
  revision: b2dee24b0b66ce0ee43eaec6aab2e0686e58049f
  specs:
    rails-api (0.2.1)
      actionpack (>= 3.2.11)
      railties (>= 3.2.11)

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.1.1)
      actionpack (= 4.1.1)
      actionview (= 4.1.1)
      mail (~> 2.5.4)
    actionpack (4.1.1)
      actionview (= 4.1.1)
      activesupport (= 4.1.1)
      rack (~> 1.5.2)
      rack-test (~> 0.6.2)
    actionview (4.1.1)
      activesupport (= 4.1.1)
      builder (~> 3.1)
      erubis (~> 2.7.0)
    activemodel (4.1.1)
      activesupport (= 4.1.1)
      builder (~> 3.1)
    activerecord (4.1.1)
      activemodel (= 4.1.1)
      activesupport (= 4.1.1)
      arel (~> 5.0.0)
    activesupport (4.1.1)
      i18n (~> 0.6, >= 0.6.9)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.1)
      tzinfo (~> 1.1)
    addressable (2.3.6)
    apipie-rails (0.2.6)
      json
    arel (5.0.1.20140414130214)
    bson (2.3.0)
    bson_ext (1.5.1)
    builder (3.2.2)
    connection_pool (2.0.0)
    diff-lcs (1.2.5)
    erubis (2.7.0)
    factory_girl (4.4.0)
      activesupport (>= 3.0.0)
    faraday (0.9.0)
      multipart-post (>= 1.2, < 3)
    hike (1.2.3)
    i18n (0.6.11)
    json (1.8.1)
    koala (1.10.1)
      addressable
      faraday
      multi_json
    mail (2.5.4)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.25.1)
    minitest (5.4.1)
    mongoid_alize (0.4.3)
      mongoid (>= 2.4)
    moped (2.0.0)
      bson (~> 2.2)
      connection_pool (~> 2.0)
      optionable (~> 0.2.0)
    multi_json (1.10.1)
    multipart-post (2.0.0)
    optionable (0.2.0)
    origin (2.1.1)
    polyglot (0.3.5)
    rack (1.5.2)
    rack-attack (4.1.0)
      rack
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (4.1.1)
      actionmailer (= 4.1.1)
      actionpack (= 4.1.1)
      actionview (= 4.1.1)
      activemodel (= 4.1.1)
      activerecord (= 4.1.1)
      activesupport (= 4.1.1)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.1.1)
      sprockets-rails (~> 2.0)
    rails_12factor (0.0.2)
      rails_serve_static_assets
      rails_stdout_logging
    rails_serve_static_assets (0.0.2)
    rails_stdout_logging (0.0.3)
    railties (4.1.1)
      actionpack (= 4.1.1)
      activesupport (= 4.1.1)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (10.3.2)
    rspec-core (3.1.2)
      rspec-support (~> 3.1.0)
    rspec-expectations (3.1.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.1.0)
    rspec-mocks (3.1.0)
      rspec-support (~> 3.1.0)
    rspec-rails (3.1.0)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 3.1.0)
      rspec-expectations (~> 3.1.0)
      rspec-mocks (~> 3.1.0)
      rspec-support (~> 3.1.0)
    rspec-support (3.1.0)
    spring (1.1.3)
    sprockets (2.12.2)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-rails (2.1.4)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (~> 2.8)
    thor (0.19.1)
    thread_safe (0.3.4)
    tilt (1.4.1)
    treetop (1.4.15)
      polyglot
      polyglot (>= 0.3.1)
    tzinfo (1.2.2)
      thread_safe (~> 0.1)
    yard (0.8.7.4)

PLATFORMS
  ruby

DEPENDENCIES
  apipie-rails
  bson_ext
  factory_girl
  koala (~> 1.10.0rc)
  mongoid!
  mongoid_alize
  rack-attack
  rails (= 4.1.1)
  rails-api!
  rails_12factor
  rspec-rails
  spring
  versionist!

My routes

Rails.application.routes.draw do
  apipie
  root :to => "api#index"
  match "/404", to: "errors#not_found", via: [:get, :post, :delete, :put]
  match "/500", to: "errors#exception", via: [:get, :post, :delete, :put]
  match "/400", to: "errors#bad_data", via: [:get, :post, :delete, :put]
  match "/401", to: "errors#unauthorized", via: [:get, :post, :delete, :put]
  match "/403", to: "errors#forbidden", via: [:get, :post, :delete, :put]

#  scope "api" do
    match '/', :to => 'api#index', via: [:get]
    api_version(:module => "V1", :path => {:value => "v1"}, :default => true) do
       match '/user', :to => 'user#user', via: [:get]

       match '/user', :to => 'user#create', via: [:post]
       match '/user/fb', :to => 'user#create_fb', via: [:post]
       match '/user/fb/auth', :to => 'user#authenticate_fb', via: [:post]

       match '/user', :to => 'user#update', via: [:put]       

       match '/user/auth', :to => 'user#login', via: [:post]
       match '/user/auth', :to => 'user#logout', via: [:delete]
       match '/user/goals', :to => 'user#goals', via: [:get]

       match '/user/sport_profile', :to => 'user#sport_profile', via: [:get]
       match '/user/sport_profile', :to => 'user#update_sport_profile', via: [:post]

       match '/exercise', :to => 'exercise#exercises', via: [:get]
       match '/exercise/:id', :to => 'exercise#exercise', via: [:get]

       match '/program', :to => 'program#generate', via: [:post]

      end
#  end

  match '*a', :to => 'errors#not_found', via: [:get, :post, :delete, :put]  
end

My /controllers/v1/exercise_controller.rb

class V1::ExerciseController < V1::BaseController

  def exercises
    render json: Exercise.all, status: :ok
  end

  def exercise
    render json: Exercise.find(params[:id]), status: :ok
  end

end

My /spec/requests/v1/exercises_controller_spec.rb

require 'spec_helper'

module V1
  describe ExerciseController do
    it "test" do
      get '/v1/exercises'
      assert_response 200
    end
  end
end

And I am getting

bash-3.2$ bundle exec rspec
/Users/glhfmf/apps/fitmeup/spec/requests/v1/exercise_controller_spec.rb:4:in `<module:V1>': uninitialized constant V1::ExerciseController (NameError)
    from /Users/glhfmf/apps/fitmeup/spec/requests/v1/exercise_controller_spec.rb:3:in `<top (required)>'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/lib/rspec/core/configuration.rb:1105:in `load'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/lib/rspec/core/configuration.rb:1105:in `each'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/lib/rspec/core/runner.rb:96:in `setup'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/lib/rspec/core/runner.rb:84:in `run'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/lib/rspec/core/runner.rb:69:in `run'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/lib/rspec/core/runner.rb:37:in `invoke'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.2/exe/rspec:4:in `<top (required)>'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/bin/rspec:23:in `load'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/bin/rspec:23:in `<main>'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
    from /Users/glhfmf/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'
bash-3.2$ 

Tried a ton of different options and still getting either Undefined constant V1 or Undefined constant V1::ControllerName for different controllers. Any help would be very useful! Thank you!

Atamanov commented 10 years ago

I believe i've found a solution. It seems to be Rspec configuration problem rather then versionist issue. One should require 'rails_helper' instead of 'spec_helper'. May be it would be of some use. Here it is my rails_helper for configuration without ActiveRecord

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
# ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  #config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  #config.use_transactional_fixtures = true

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!
end