Rspec未初始化常量Api :: V1:控制器名称

时间:2014-10-02 05:48:37

标签: ruby-on-rails rspec

您好我在stackoverflow上看到了类似的问题,没有一个答案解决了我的问题

我的实际控制器在myapp / app / controllers / Api / V1 / events_controller.rb

class Api::V1::EventsController < ApplicationController

    def index

    end
    def create
        @event = Event.new(event_params)
        if @event.save
            render json: @event
        else
            render :status => 401,json: {:errors=> @event.errors }
        end
    end
    private
        def event_params
            params.require(:event).permit(:name,:desc)
        end
end

当我运行我的规范:bundle exec rspec或者bundle exec guard

我得到以下错误

rigel@rigel:/var/www/rails4/a/simplebackend$ bundle exec rake
/home/rigel/.rbenv/versions/2.0.0-p0/bin/ruby -I/home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib:/home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-support-3.1.1/lib /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
/var/www/rails4/a/simple-auth-devise-basic-backend/spec/controllers/api/v1/events_controller_spec.rb:8:in `<top (required)>': uninitialized constant Api::V1::EventsController (NameError)
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib/rspec/core/configuration.rb:1105:in `load'
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib/rspec/core/configuration.rb:1105:in `each'
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib/rspec/core/runner.rb:96:in `setup'
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib/rspec/core/runner.rb:84:in `run'
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib/rspec/core/runner.rb:69:in `run'
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib/rspec/core/runner.rb:37:in `invoke'
    from /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/exe/rspec:4:in `<main>'
/home/rigel/.rbenv/versions/2.0.0-p0/bin/ruby -I/home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/lib:/home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-support-3.1.1/lib /home/rigel/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.5/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb failed

-my event controller spec看起来像这样

require 'rails_helper'
# binding.pry
RSpec.describe Api::V1::EventsController, :type => :controller do
end

rails_helper看起来像这样

# 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'
# Add additional requires below this line. Rails is not loaded until this point!

# 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`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# 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

我的spec_helper看起来像这样

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
    # This option will default to `true` in RSpec 4. It makes the `description`
    # and `failure_message` of custom matchers include text for helper methods
    # defined using `chain`, e.g.:
    # be_bigger_than(2).and_smaller_than(4).description
    #   # => "be bigger than 2 and smaller than 4"
    # ...rather than:
    #   # => "be bigger than 2"
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
  config.mock_with :rspec do |mocks|
    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended, and will default to
    # `true` in RSpec 4.
    mocks.verify_partial_doubles = true
  end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
  # These two settings work together to allow you to limit a spec run
  # to individual examples or groups you care about by tagging them with
  # `:focus` metadata. When nothing is tagged with `:focus`, all examples
  # get run.
  config.filter_run :focus
  config.run_all_when_everything_filtered = true

  # Limits the available syntax to the non-monkey patched syntax that is recommended.
  # For more details, see:
  #   - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
  #   - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
  #   - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
  config.disable_monkey_patching!

  # Many RSpec users commonly either run the entire suite or an individual
  # file, and it's useful to allow more verbose output when running an
  # individual spec file.
  if config.files_to_run.one?
    # Use the documentation formatter for detailed output,
    # unless a formatter has already been configured
    # (e.g. via a command-line flag).
    config.default_formatter = 'doc'
  end

  # Print the 10 slowest examples and example groups at the
  # end of the spec run, to help surface which specs are running
  # particularly slow.
  config.profile_examples = 10

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = :random

  # Seed global randomization in this process using the `--seed` CLI option.
  # Setting this allows you to use `--seed` to deterministically reproduce
  # test failures related to randomization by passing the same `--seed` value
  # as the one that triggered the failure.
  Kernel.srand config.seed
=end
end

我的gemfile锁看起来像这样

GIT
  remote: git://github.com/gregbell/active_admin.git
  revision: 5e114b7d9cac1e8a4ad83e972b24c0cb5b491fce
  specs:
    activeadmin (1.0.0.pre)
      arbre (~> 1.0, >= 1.0.2)
      bourbon
      coffee-rails
      formtastic (~> 2.3)
      inherited_resources (~> 1.4.1)
      jquery-rails
      jquery-ui-rails (~> 5.0)
      kaminari (~> 0.15)
      rails (>= 3.2, < 4.2)
      ransack (~> 1.3)
      sass-rails

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.1.6)
      actionpack (= 4.1.6)
      actionview (= 4.1.6)
      mail (~> 2.5, >= 2.5.4)
    actionpack (4.1.6)
      actionview (= 4.1.6)
      activesupport (= 4.1.6)
      rack (~> 1.5.2)
      rack-test (~> 0.6.2)
    actionview (4.1.6)
      activesupport (= 4.1.6)
      builder (~> 3.1)
      erubis (~> 2.7.0)
    active_model_serializers (0.9.0)
      activemodel (>= 3.2)
    activemodel (4.1.6)
      activesupport (= 4.1.6)
      builder (~> 3.1)
    activerecord (4.1.6)
      activemodel (= 4.1.6)
      activesupport (= 4.1.6)
      arel (~> 5.0.0)
    activesupport (4.1.6)
      i18n (~> 0.6, >= 0.6.9)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.1)
      tzinfo (~> 1.1)
    arbre (1.0.2)
      activesupport (>= 3.0.0)
    arel (5.0.1.20140414130214)
    axiom-types (0.1.1)
      descendants_tracker (~> 0.0.4)
      ice_nine (~> 0.11.0)
      thread_safe (~> 0.3, >= 0.3.1)
    bcrypt (3.1.7-x86-mingw32)
    binding_of_caller (0.7.2)
      debug_inspector (>= 0.0.1)
    bootstrap-sass (3.1.1.1)
      sass (~> 3.2)
    bootswatch-rails (3.2.4)
      railties (>= 3.1)
    bourbon (3.2.3)
      sass (~> 3.2)
      thor
    bower-rails (0.7.3)
    builder (3.2.2)
    cancan (1.6.10)
    capybara (2.4.1)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      xpath (~> 2.0)
    celluloid (0.16.0)
      timers (~> 4.0.0)
    chunky_png (1.3.1)
    cliver (0.3.2)
    coderay (1.1.0)
    coercible (1.0.0)
      descendants_tracker (~> 0.0.1)
    coffee-rails (4.0.1)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0, < 5.0)
    coffee-script (2.3.0)
      coffee-script-source
      execjs
    coffee-script-source (1.8.0)
    columnize (0.8.9)
    compass (0.12.7)
      chunky_png (~> 1.2)
      fssm (>= 0.2.7)
      sass (~> 3.2.19)
    cucumber (1.3.16)
      builder (>= 2.1.2)
      diff-lcs (>= 1.1.3)
      gherkin (~> 2.12)
      multi_json (>= 1.7.5, < 2.0)
      multi_test (>= 0.1.1)
    cucumber-rails (1.4.0)
      capybara (>= 1.1.2)
      cucumber (>= 1.2.0)
      nokogiri (>= 1.5.0)
      rails (>= 3.0.0)
    database_cleaner (1.3.0)
    debug_inspector (0.0.2)
    debugger (1.6.8)
      columnize (>= 0.3.1)
      debugger-linecache (~> 1.2.0)
      debugger-ruby_core_source (~> 1.3.5)
    debugger-linecache (1.2.0)
    debugger-ruby_core_source (1.3.5)
    descendants_tracker (0.0.4)
      thread_safe (~> 0.3, >= 0.3.1)
    devise (3.3.0)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 3.2.6, < 5)
      thread_safe (~> 0.1)
      warden (~> 1.2.3)
    diff-lcs (1.2.5)
    em-synchrony (1.0.3)
      eventmachine (>= 1.0.0.beta.1)
    em-websocket (0.5.1)
      eventmachine (>= 0.12.9)
      http_parser.rb (~> 0.6.0)
    ember-source (1.7.0)
      handlebars-source (~> 1.0)
    equalizer (0.0.9)
    erubis (2.7.0)
    ethon (0.7.1)
      ffi (>= 1.3.0)
    eventmachine (1.0.3-x86-mingw32)
    execjs (2.2.1)
    factory_girl (4.4.0)
      activesupport (>= 3.0.0)
    factory_girl_json (0.0.2)
      activesupport (>= 3)
      database_cleaner
      factory_girl (>= 4.1)
      json
    factory_girl_rails (4.4.1)
      factory_girl (~> 4.4.0)
      railties (>= 3.0.0)
    faker (1.4.3)
      i18n (~> 0.5)
    ffi (1.9.3-x86-mingw32)
    formatador (0.2.5)
    formtastic (2.3.1)
      actionpack (>= 3.0)
    fssm (0.2.10)
    gherkin (2.12.2-x86-mingw32)
      multi_json (~> 1.3)
    guard (2.6.1)
      formatador (>= 0.2.4)
      listen (~> 2.7)
      lumberjack (~> 1.0)
      pry (>= 0.9.12)
      thor (>= 0.18.1)
    guard-cucumber (1.4.1)
      cucumber (>= 1.2.0)
      guard (>= 1.1.0)
    guard-livereload (2.3.0)
      em-websocket (~> 0.5)
      guard (~> 2.0)
      multi_json (~> 1.8)
    guard-rails (0.5.3)
      guard (~> 2.0)
    guard-rspec (4.3.1)
      guard (~> 2.1)
      rspec (>= 2.14, < 4.0)
    handlebars-source (1.3.0)
    has_scope (0.6.0.rc)
      actionpack (>= 3.2, < 5)
      activesupport (>= 3.2, < 5)
    hashie (3.3.1)
    hashie_rails (0.0.1)
      hashie (>= 3.0)
      rails (~> 4.0)
    hike (1.2.3)
    hitimes (1.2.2)
    http_parser.rb (0.6.0)
    httpclient (2.4.0)
    i18n (0.6.11)
    ice_nine (0.11.0)
    inherited_resources (1.4.1)
      has_scope (~> 0.6.0.rc)
      responders (~> 1.0.0.rc)
    interception (0.5)
    jquery-rails (3.1.2)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    jquery-ui-rails (5.0.0)
      railties (>= 3.2.16)
    json (1.8.1)
    kaminari (0.16.1)
      actionpack (>= 3.0.0)
      activesupport (>= 3.0.0)
    listen (2.7.9)
      celluloid (>= 0.15.2)
      rb-fsevent (>= 0.9.3)
      rb-inotify (>= 0.9)
    lumberjack (1.0.9)
    mail (2.6.1)
      mime-types (>= 1.16, < 3)
    method_source (0.8.2)
    mime-types (2.3)
    mini_portile (0.6.0)
    minitest (5.4.1)
    multi_json (1.10.1)
    multi_test (0.1.1)
    nokogiri (1.6.3.1-x86-mingw32)
      mini_portile (= 0.6.0)
    orm_adapter (0.5.0)
    pg (0.17.1-x86-mingw32)
    poltergeist (1.5.1)
      capybara (~> 2.1)
      cliver (~> 0.3.1)
      multi_json (~> 1.0)
      websocket-driver (>= 0.2.0)
    polyamorous (1.1.0)
      activerecord (>= 3.0)
    populator (1.0.0)
    pry (0.10.1-x86-mingw32)
      coderay (~> 1.1.0)
      method_source (~> 0.8.1)
      slop (~> 3.4)
      win32console (~> 1.3)
    pry-debugger (0.2.3)
      debugger (~> 1.3)
      pry (>= 0.9.10, < 0.11.0)
    pry-rails (0.3.2)
      pry (>= 0.9.10)
    pry-rescue (1.4.1)
      interception (>= 0.5)
      pry
    pry-stack_explorer (0.4.9.1)
      binding_of_caller (>= 0.7)
      pry (>= 0.9.11)
    pusher (0.14.1)
      httpclient (~> 2.3)
      multi_json (~> 1.0)
      signature (~> 0.1.6)
    rack (1.5.2)
    rack-cors (0.2.9)
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (4.1.6)
      actionmailer (= 4.1.6)
      actionpack (= 4.1.6)
      actionview (= 4.1.6)
      activemodel (= 4.1.6)
      activerecord (= 4.1.6)
      activesupport (= 4.1.6)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.1.6)
      sprockets-rails (~> 2.0)
    railties (4.1.6)
      actionpack (= 4.1.6)
      activesupport (= 4.1.6)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (10.3.2)
    ransack (1.3.0)
      actionpack (>= 3.0)
      activerecord (>= 3.0)
      activesupport (>= 3.0)
      i18n
      polyamorous (~> 1.1)
    rb-fsevent (0.9.4)
    rb-inotify (0.9.5)
      ffi (>= 0.5.0)
    rdoc (4.1.2)
      json (~> 1.4)
    responders (1.0.0)
      railties (>= 3.2, < 5)
    rolify (3.4.1)
    rspec (3.1.0)
      rspec-core (~> 3.1.0)
      rspec-expectations (~> 3.1.0)
      rspec-mocks (~> 3.1.0)
    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)
    sass (3.2.19)
    sass-rails (4.0.3)
      railties (>= 4.0.0, < 5.0)
      sass (~> 3.2.0)
      sprockets (~> 2.8, <= 2.11.0)
      sprockets-rails (~> 2.0)
    sdoc (0.4.1)
      json (~> 1.7, >= 1.7.7)
      rdoc (~> 4.0)
    shoulda-matchers (2.7.0)
      activesupport (>= 3.0.0)
    signature (0.1.7)
    slop (3.6.0)
    sprockets (2.11.0)
      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)
    sqlite3 (1.3.9-x86-mingw32)
    thor (0.19.1)
    thread_safe (0.3.4)
    tilt (1.4.1)
    timers (4.0.1)
      hitimes
    typhoeus (0.6.9)
      ethon (>= 0.7.1)
    tzinfo (1.2.2)
      thread_safe (~> 0.1)
    tzinfo-data (1.2014.7)
      tzinfo (>= 1.0.0)
    uglifier (2.5.3)
      execjs (>= 0.3.0)
      json (>= 1.8.0)
    virtus (1.0.3)
      axiom-types (~> 0.1)
      coercible (~> 1.0)
      descendants_tracker (~> 0.0, >= 0.0.3)
      equalizer (~> 0.0, >= 0.0.9)
    warden (1.2.3)
      rack (>= 1.0)
    websocket-driver (0.3.4)
    win32console (1.3.2-x86-mingw32)
    xpath (2.0.0)
      nokogiri (~> 1.3)

PLATFORMS
  x86-mingw32

DEPENDENCIES
  active_model_serializers
  activeadmin!
  bootstrap-sass (~> 3.1.1)
  bootswatch-rails
  bower-rails (~> 0.7.3)
  cancan
  capybara
  compass
  cucumber-rails
  database_cleaner
  debugger
  delayed_job_active_record
  devise
  em-synchrony
  ember-source (~> 1.7.0)
  factory_girl_json
  factory_girl_rails
  faker
  foreman
  guard
  guard-cucumber
  guard-livereload
  guard-rails
  guard-rspec
  hashie_rails
  jquery-rails
  pg
  poltergeist
  populator
  pry-debugger
  pry-rails
  pry-rescue
  pry-stack_explorer
  pusher
  rack-cors
  rails (= 4.1.6)
  rolify
  rspec-rails
  sass-rails (~> 4.0.0)
  sdoc
  shoulda-matchers
  sqlite3
  typhoeus
  tzinfo-data
  uglifier (>= 1.3.0)
  unicorn
  virtus
  wdm

1 个答案:

答案 0 :(得分:3)

你的路径是

myapp/app/controllers/Api/V1/events_controller.rb 

路径中的首都A和V让我困惑。当路径中的大写字母对操作系统敏感时,我遇到了类似的问题。尝试使用

myapp/app/controllers/api/v1/events_controller.rb

没有大写字母