1

I am having problems running Rspec:

My RSpec tests run properly with bundle exec rake spec, but I get a missing gem error with bundle exec rspec spec.

I am using Rails 4.1.0 and Ruby 1.9.3p484 (2013-11-22) [i386-mingw32] on a Windows 7 operating system with Railsinstaller.


C:\RailsInstaller\Ruby1.9.3\myapp>bundle exec rspec spec
bundler: command not found: rspec
Install missing gem executables with `bundle install`

When I try to narrow my spec tests with a command like bundle exec rspec spec/models/project_spec.rb all of my tests run instead, slowing down my test driven development considerably. I have repeated this issue on multiple Rails projects with spec tests, including from projects with tests that I have cloned from Github. Downgrading to Rails 4.0.0 from Rails 4.0.1 did not solve the problem. My Bundler version is 1.6.2.

I have the rspec-rails gem installed at C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems, and installation appears to work fine:


C:\RailsInstaller\Ruby1.9.3\myapp>gem install rspec-rails
Successfully installed rspec-rails-2.14.2
Parsing documentation for rspec-rails-2.14.2
Done installing documentation for rspec-rails after 1 seconds
1 gem installed

Sample of working spec test using bundle exec rake spec



    C:\RailsInstaller\Ruby1.9.3\myapp>bundle exec
    rake spec ./spec/models/project_spec.rb
    C:/RailsInstaller/Ruby1.9.3/bin/ruby.exe -S rspec ./spec/controllers/projects_co
    ntroller_spec.rb ./spec/models/project_spec.rb
    ...FF..

    Failures:

      1) ProjectsController#create with invalid parameters doesn't create a record t
    o the database if title is missing
         Failure/Error: post :create, project: {title: "",
         NoMethodError:
           undefined method `new_url' for #
         # ./app/controllers/projects_controller.rb:19:in `create'
         # ./spec/controllers/projects_controller_spec.rb:48:in `invalid_params_titl
    e'
         # ./spec/controllers/projects_controller_spec.rb:58:in `block (5 levels) in
     '
         # ./spec/controllers/projects_controller_spec.rb:58:in `block (4 levels) in
     '

      2) ProjectsController#create with invalid parameters doesn't create a record t
    o the database if description is missing
         Failure/Error: post :create, project: {title: "some valid title",
         NoMethodError:
           undefined method `new_url' for #
         # ./app/controllers/projects_controller.rb:19:in `create'
         # ./spec/controllers/projects_controller_spec.rb:53:in `invalid_params_desc
    ription'
         # ./spec/controllers/projects_controller_spec.rb:62:in `block (5 levels) in
     '
         # ./spec/controllers/projects_controller_spec.rb:62:in `block (4 levels) in
     '

    Finished in 0.19601 seconds
    7 examples, 2 failures

    Failed examples:

    rspec ./spec/controllers/projects_controller_spec.rb:57 # ProjectsController#cre
    ate with invalid parameters doesn't create a record to the database if title is
    missing
    rspec ./spec/controllers/projects_controller_spec.rb:61 # ProjectsController#cre
    ate with invalid parameters doesn't create a record to the database if descripti
    on is missing

    Randomized with seed 880

    C:/RailsInstaller/Ruby1.9.3/bin/ruby.exe -S rspec ./spec/controllers/projects_co
    ntroller_spec.rb ./spec/models/project_spec.rb failed

My Gemfile:



    source 'https://rubygems.org'

    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails', '4.1.0'
    # Use sqlite3 as the database for Active Record
    gem 'sqlite3'
    # Use SCSS for stylesheets
    gem 'sass-rails', '~> 4.0.3'
    # Use Uglifier as compressor for JavaScript assets
    gem 'uglifier', '>= 1.3.0'
    # Use CoffeeScript for .js.coffee assets and views
    gem 'coffee-rails', '~> 4.0.0'
    # See https://github.com/sstephenson/execjs#readme for more supported runtimes
    # gem 'therubyracer',  platforms: :ruby

    # Use jquery as the JavaScript library
    gem 'jquery-rails'
    # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
    gem 'turbolinks'
    # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
    gem 'jbuilder', '~> 2.0'
    # bundle exec rake doc:rails generates the API under doc/api.
    gem 'sdoc', '~> 0.4.0',          group: :doc

    gem 'bootstrap-sass'
    gem 'haml-rails'
    gem 'quiet_assets'
    gem 'thin'
    gem 'simple_form'
    gem 'hirb'

    #This didn't work either:
    #gem 'rspec-rails', group: [:development, :test]


    group :development, :test do
      gem 'rspec'
      gem 'rspec-rails'
      gem 'faker'
      gem "factory_girl_rails"
      gem "capybara"
      # gem "capybara-webkit"
      gem 'selenium-webdriver', '~> 2.41.0'
      gem "database_cleaner"
      gem "rails-erd" #use rake erd to create a .dot file you can view current ERD with graphviz
    end

    # Use ActiveModel has_secure_password
    # gem 'bcrypt', '~> 3.1.7'

    # Use unicorn as the app server
    # gem 'unicorn'

    # Use Capistrano for deployment
    # gem 'capistrano-rails', group: :development

    # Use debugger
    # gem 'debugger', group: [:development, :test]

    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin]

My Gemfile.lock, which I deleted and ran bundle install on multiple times:



    GEM
      remote: https://rubygems.org/
      specs:
        actionmailer (4.1.0)
          actionpack (= 4.1.0)
          actionview (= 4.1.0)
          mail (~> 2.5.4)
        actionpack (4.1.0)
          actionview (= 4.1.0)
          activesupport (= 4.1.0)
          rack (~> 1.5.2)
          rack-test (~> 0.6.2)
        actionview (4.1.0)
          activesupport (= 4.1.0)
          builder (~> 3.1)
          erubis (~> 2.7.0)
        activemodel (4.1.0)
          activesupport (= 4.1.0)
          builder (~> 3.1)
        activerecord (4.1.0)
          activemodel (= 4.1.0)
          activesupport (= 4.1.0)
          arel (~> 5.0.0)
        activesupport (4.1.0)
          i18n (~> 0.6, >= 0.6.9)
          json (~> 1.7, >= 1.7.7)
          minitest (~> 5.1)
          thread_safe (~> 0.1)
          tzinfo (~> 1.1)
        arel (5.0.1.20140414130214)
        bootstrap-sass (3.1.1.1)
          sass (~> 3.2)
        builder (3.2.2)
        capybara (2.2.1)
          mime-types (>= 1.16)
          nokogiri (>= 1.3.3)
          rack (>= 1.0.0)
          rack-test (>= 0.5.4)
          xpath (~> 2.0)
        childprocess (0.5.3)
          ffi (~> 1.0, >= 1.0.11)
        choice (0.1.6)
        coffee-rails (4.0.1)
          coffee-script (>= 2.2.0)
          railties (>= 4.0.0, = 3.0.0)
        factory_girl_rails (4.4.1)
          factory_girl (~> 4.4.0)
          railties (>= 3.0.0)
        faker (1.3.0)
          i18n (~> 0.5)
        ffi (1.9.3-x86-mingw32)
        haml (4.0.5)
          tilt
        haml-rails (0.5.3)
          actionpack (>= 4.0.1)
          activesupport (>= 4.0.1)
          haml (>= 3.1, = 4.0.1)
        hike (1.2.3)
        hirb (0.7.1)
        i18n (0.6.9)
        jbuilder (2.0.7)
          activesupport (>= 3.0.0,  1.2)
        jquery-rails (3.1.0)
          railties (>= 3.0, = 0.14,  1.16)
          treetop (~> 1.4.8)
        mime-types (1.25.1)
        mini_portile (0.5.3)
        minitest (5.3.3)
        multi_json (1.10.0)
        nokogiri (1.6.1-x86-mingw32)
          mini_portile (~> 0.5.0)
        polyglot (0.3.4)
        quiet_assets (1.0.2)
          railties (>= 3.1, = 1.0)
        rails (4.1.0)
          actionmailer (= 4.1.0)
          actionpack (= 4.1.0)
          actionview (= 4.1.0)
          activemodel (= 4.1.0)
          activerecord (= 4.1.0)
          activesupport (= 4.1.0)
          bundler (>= 1.3.0,  2.0)
        rails-erd (1.1.0)
          activerecord (>= 3.0)
          activesupport (>= 3.0)
          choice (~> 0.1.6)
          ruby-graphviz (~> 1.0.4)
        railties (4.1.0)
          actionpack (= 4.1.0)
          activesupport (= 4.1.0)
          rake (>= 0.8.7)
          thor (>= 0.18.1,  1.4)
        rspec (2.14.1)
          rspec-core (~> 2.14.0)
          rspec-expectations (~> 2.14.0)
          rspec-mocks (~> 2.14.0)
        rspec-core (2.14.8)
        rspec-expectations (2.14.5)
          diff-lcs (>= 1.1.3, = 3.0)
          activemodel (>= 3.0)
          activesupport (>= 3.0)
          railties (>= 3.0)
          rspec-core (~> 2.14.0)
          rspec-expectations (~> 2.14.0)
          rspec-mocks (~> 2.14.0)
        ruby-graphviz (1.0.9)
        rubyzip (1.1.3)
        sass (3.2.19)
        sass-rails (4.0.3)
          railties (>= 4.0.0,  3.2.0)
          sprockets (~> 2.8,  2.0)
        sdoc (0.4.0)
          json (~> 1.8)
          rdoc (~> 4.0, = 0.5.0)
          multi_json (~> 1.0)
          rubyzip (~> 1.0)
          websocket (~> 1.0.4)
        simple_form (3.0.2)
          actionpack (~> 4.0)
          activemodel (~> 4.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.3)
          actionpack (>= 3.0)
          activesupport (>= 3.0)
          sprockets (~> 2.8)
        sqlite3 (1.3.9-x86-mingw32)
        thin (1.6.2)
          daemons (>= 1.0.9)
          eventmachine (>= 1.0.0)
          rack (>= 1.0.0)
        thor (0.19.1)
        thread_safe (0.3.3)
        tilt (1.4.1)
        treetop (1.4.15)
          polyglot
          polyglot (>= 0.3.1)
        turbolinks (2.2.2)
          coffee-rails
        tzinfo (1.1.0)
          thread_safe (~> 0.1)
        tzinfo-data (1.2014.2)
          tzinfo (>= 1.0.0)
        uglifier (2.5.0)
          execjs (>= 0.3.0)
          json (>= 1.8.0)
        websocket (1.0.7)
        xpath (2.0.0)
          nokogiri (~> 1.3)

    PLATFORMS
      x86-mingw32

    DEPENDENCIES
      bootstrap-sass
      capybara
      coffee-rails (~> 4.0.0)
      database_cleaner
      factory_girl_rails
      faker
      haml-rails
      hirb
      jbuilder (~> 2.0)
      jquery-rails
      quiet_assets
      rails (= 4.1.0)
      rails-erd
      rspec
      rspec-rails
      sass-rails (~> 4.0.3)
      sdoc (~> 0.4.0)
      selenium-webdriver (~> 2.41.0)
      simple_form
      sqlite3
      thin
      turbolinks
      tzinfo-data
      uglifier (>= 1.3.0)

Kelsey Hannan
  • 2,857
  • 2
  • 30
  • 46

2 Answers2

1

Problem 1

The version of ruby you need to use or are using is not the one rspec is using. rspec is path'd to /usr/bin/rspec whose first line is regrettably

#!/usr/bin/ruby

which ties your platform to whatever version /usr/bin/ruby is. Fix this either by changing the file or creating a new rspec earlier in the path:

#!/usr/bin/env ruby

(On some systems, it must instead be #!/bin/env ruby).

Problem 2

To use rspec with user-local gems (ie, in $HOME/.gem) you must specify $GEM_PATH. Basically rspec doesn't honor your $HOME/.gemrc file.

Perhaps bundle exec solves the above problems, but for me it created more.

Otheus
  • 785
  • 10
  • 18
0

Could you please try this.

bundle exec "rspec spec"

OR

bundle exec rspec
Khushal Dave
  • 507
  • 5
  • 19
  • 1
    Both give back the same error: C:\RailsInstaller\Ruby1.9.3\myapp>bundle exec "rspec spec" bundler: command not found: rspec spec Install missing gem executables with `bundle install` – Kelsey Hannan May 09 '14 at 09:26
  • I should mention that at some point rspec spec did work. Something changed recently, and I can't pin down what that could be, but I suspect it involves the paths that bundler and rake are looking to run their respective commands. – Kelsey Hannan May 09 '14 at 09:31
  • No. but Bundle show displays * rspec-core (2.14.8) * rspec-expectations (2.14.5) * rspec-mocks (2.14.6) * rspec-rails (2.14.2) – Kelsey Hannan May 09 '14 at 09:32
  • uninstall all other versions except the one listed in the Gemfile and upgrade the rspec version in the Gemfile – Khushal Dave May 09 '14 at 09:34
  • When you have more than one version installed on your system. so while using **rspec** will load the latest one... it might mot match with one specified in the gemfile – Khushal Dave May 09 '14 at 09:38