运行rspec spec /不会产生正确的输出

时间:2012-12-19 02:41:29

标签: ruby-on-rails ruby rspec

我正在做Michael Hartl的Ruby On Rails 3教程(通过示例学习Rails)。

我在第3章,我的输出不是迈克尔在书中的内容。有人可以帮忙吗?

admin@new-host:~/Desktop/sample_app$ rspec spec/
Exception encountered: #<NameError: uninitialized constant PagesController>
backtrace:
/Users/admin/Desktop/sample_app/spec/controllers/pages_controller_spec.rb:3:in `<top (required)>'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/test_framework/rspec.rb:11:in `run_tests'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:13:in `block in run'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/forker.rb:21:in `block in initialize'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/forker.rb:18:in `fork'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/forker.rb:18:in `initialize'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/server.rb:48:in `run'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1548:in `perform_without_block'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1508:in `perform'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1586:in `block (2 levels) in main_loop'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1582:in `loop'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1582:in `block in main_loop'

spec_helper.rb:

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.
  ENV["RAILS_ENV"] ||= 'test'
  unless defined?(Rails)
    require File.dirname(__FILE__) + "/../config/environment"
  end

  require 'rspec/rails'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

  RSpec.configure do |config|
    # == Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    # 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

    ActiveSupport::Dependencies.clear

  end


end

Spork.each_run do
  # This code will be run each time you run your specs.

end

基于Ismael的建议。我做了这个并获得了以下输出:

admin@new-host:~/Desktop/sample_app$ rspec --pattern spec/*_spec.rb
Exception encountered: #<NameError: uninitialized constant PagesController>
backtrace:
/Users/admin/Desktop/sample_app/spec/controllers/pages_controller_spec.rb:3:in `<top (required)>'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/test_framework/rspec.rb:11:in `run_tests'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:13:in `block in run'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/forker.rb:21:in `block in initialize'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/forker.rb:18:in `fork'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/forker.rb:18:in `initialize'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/spork-0.9.2/lib/spork/server.rb:48:in `run'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1548:in `perform_without_block'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1508:in `perform'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1586:in `block (2 levels) in main_loop'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1582:in `loop'
/Users/admin/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/drb/drb.rb:1582:in `block in main_loop'

admin@new-host:~/Desktop/sample_app$ autotest
loading autotest/rails_rspec2
/Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/ZenTest-4.8.3/lib/autotest.rb:315:in `load': /Users/admin/Desktop/sample_app/.autotest:3: unterminated string meets end of file (SyntaxError)
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/ZenTest-4.8.3/lib/autotest.rb:315:in `block in initialize'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/ZenTest-4.8.3/lib/autotest.rb:314:in `each'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/ZenTest-4.8.3/lib/autotest.rb:314:in `initialize'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-core-2.12.2/lib/autotest/rspec2.rb:13:in `initialize'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/rspec-rails-2.12.0/lib/autotest/rails_rspec2.rb:31:in `initialize'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/ZenTest-4.8.3/lib/autotest.rb:244:in `new'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/ZenTest-4.8.3/lib/autotest.rb:244:in `run'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/gems/ZenTest-4.8.3/bin/autotest:6:in `<top (required)>'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/bin/autotest:19:in `load'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/bin/autotest:19:in `<main>'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
    from /Users/admin/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'

2 个答案:

答案 0 :(得分:1)

首先,你需要在项目目录中。

如果仍然无效,请尝试以这种方式运行:

rspec --pattern spec/*_spec.rb

答案 1 :(得分:1)

使用以下代码替换spec_helper中的代码:https://raw.github.com/railstutorial/sample_app/master/spec/spec_helper.rb