2

Michael Hartl 的精彩Rails 教程现在可用于 Rails 3.2。他继续使用 rspec 和 spork 概述 TDD 和 BDD,就像他在 3.0 版中所做的那样,并在 3.2 中添加了保护。在 3.0 版中,Hartl 包含有关自动测试的信息,我能够让美妙的咆哮通知正常工作。然而,在 3.2 中,他不再包含自动测试或大量咆哮信息。Guard 与 spork 配合得很好,但没有通知。在过去的几个小时里,我自己冒险使用了 Hartl 的 3.0 和一些博客文章,但试图让自动测试工作仍然会产生“LoadError”和“无法运行测试”的咆哮通知。非常感谢任何想法。我在 OS X 10.7.3 上。这是我所做的:

$ gem install autotest -v 4.4.6
$ gem install autotest-rails-pure -v 4.1.2
$ gem install autotest-fsevent -v 0.2.8
$ gem install autotest-growl -v 0.2.16

宝石文件

source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'pg', '0.12.2'
group :development, :test do 
    gem 'rspec-rails', '2.9.0'
    gem 'guard-rspec', '0.5.5'
end

group :assets do
    gem 'sass-rails','3.2.4' 
    gem 'coffee-rails', '3.2.2'
    gem 'uglifier', '1.2.3' 
end
gem 'jquery-rails', '2.0.0'
group :test do
    gem 'capybara', '1.1.2'
    gem 'rb-fsevent', '0.4.3.1', :require => false
    gem 'growl', '1.0.3'
    gem 'guard-spork', '0.3.2'
    gem 'spork', '0.9.0'
end

~/.autotest

require 'autotest/growl'
require 'autotest/fsevent'
Autotest::Growl::show_modified_files = true
Autotest::Growl::one_notification_per_run = true
Autotest::Growl::clear_terminal = false
Autotest::Growl::hide_label = true

Autotest.add_hook :initialize do |autotest|
  autotest.add_mapping(/^spec\/requests\/.*_spec\.rb$/) do
    autotest.files_matching(/^spec\/requests\/.*_spec\.rb$/)
  end  
end

$自动测试

loading autotest/rails


      --------------------------------------------------------------------------------

/Users/[me]/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit spec/requests/static_pages_spec.rb].each { |f| require f }"
/Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- spec_helper (LoadError)
    from        /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from    /Users/[me]/programing/rails/rdale_house/spec/requests/static_pages_spec.rb:1:in `<top (required)>'
    from /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from -e:1:in `block in <main>'
    from -e:1:in `each'
    from -e:1:in `<main>'

规范/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.

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

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/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

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false
  end
end

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

end
4

3 回答 3

4

我最终也只是使用 Sublime Text 2 适应了 Hartl 的工作流程,但是,我确实想确保如果我愿意的话,我可以切换到带有 Growl 通知的 Guard(据我所知,不需要自动测试,因为我相信这是 Guard 的工作),所以这就是我在个人环境设置指南中的做法。希望能对你有所帮助。我在 OSX 10.6.8 上,所以你可能需要在 10.7.3 上做一些不同的事情:

为 RSpec 测试通知设置 Growl(对于 Mac):
系统上可能已经安装了 Growl,但可能没有安装 Growlnotify。

  1. 从这里下载 Growl-1.2.2.dmg 文件
  2. 打开 dmg 文件并转到 Extras > growlnotify > growlnotify.pkg
  3. 按照向导安装 growlnotify

配置应用程序进行测试(RSpec、Cucumber with Spork 和 Guard):

$ rails generate rspec:install
$ rails generate cucumber:install

配置 Spork(用于 RSpec 和 Cucumber)

$ spork --bootstrap
$ spork cucumber --bootstrap

添加环境加载

$ subl spec/spec_helper.rb

将 Instructions 下的全部内容移动到Spork.prefork块中以仅启用一次环境加载,并添加:

config.mock_with :rspec

配置防护:

$ guard init rspec
$ guard init spork

编辑生成的默认文件,以便 Guard 在失败的测试通过后不会运行所有测试;drb标志是让 Spork 在分布式 Ruby 中运行。

$ subl Guardfile
guard 'rspec', :version => 2, :all_after_pass => false, :cli => '--drb'  do

配置在分布式 Ruby 中运行测试套件

$ subl .rspec
--drb

用 Spork 开始 Guard

$ guard

...并且您应该收到 Growl 通知。

我在Gemfile中的相关 gem 和你的几乎一样。

于 2012-07-01T22:26:11.897 回答
1

该错误表明您缺少 spec_helper 文件。你能试着跑步rails generate rspec:install然后再试一次吗?

于 2012-05-06T17:17:49.887 回答
0

我遇到了同样的问题,然后去 Guard 的 GitHub 页面查看他们的文档。相关部分在这里:https ://github.com/guard/guard#growl

似乎简单的解决方案是将咆哮宝石包含在开发组中,而您只将它包含在测试组中。我做了以下工作来让它工作:

如另一个答案中所述,安装了 Growl Notify:

为 RSpec 测试通知设置 Growl(对于 Mac):系统上可能已经安装了 Growl,但可能没有安装 Growlnotify。

  1. 从这里下载 Growl-1.2.2.dmg 文件
  2. 打开 dmg 文件并转到 Extras > growlnotify > growlnotify.pkg
  3. 按照向导安装 growlnotify

更新了我的 Gemfile(仅相关部分,其余部分与 Rails 教程匹配):

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.11.0'
  gem 'guard-rspec', '1.2.1'
  gem 'guard-spork', '1.2.0'
  gem 'childprocess', '0.3.6'
  gem 'spork', '0.9.2'
  gem 'growl', '1.0.3'
end

在终端运行:

bundle install

打开一个新的终端窗口并运行:

guard

咆哮开始工作!

于 2013-06-07T19:24:22.263 回答