这是我的设置:
rails 3.12.2,rspec 2.13.1,spork 0.9.2,mysql gem 0.3.11。我是 RoR 和 rspec 的新手。
我在后台运行 spork,没有在 spork 中报告任何问题。
当我运行我的模型测试时:
rake spec:models --trace
我得到以下结果:
Invoke spec:models (first_time)
Invoke test:prepare (first_time)
Invoke db:test:prepare (first_time)
Invoke db:abort_if_pending_migrations (first_time)
Invoke environment (first_time)
Execute environment
Invoke db:load_config (first_time)
Execute db:load_config
Execute db:abort_if_pending_migrations
Execute db:test:prepare
Invoke db:test:load (first_time)
Invoke db:test:purge (first_time)
Invoke environment
Invoke db:load_config
Execute db:test:purge
Execute db:test:load
Invoke db:test:load_schema (first_time)
Invoke db:test:purge
Execute db:test:load_schema
Invoke db:schema:load (first_time)
Invoke environment
Invoke db:load_config
Execute db:schema:load
Execute test:prepare
Execute spec:models
ruby.exe -S rspec ./spec/models/user_spec.rb
..
Finished in 0.51565 seconds
2 examples, *0 failures*
Randomized with seed 540
<-- Slave(1) run done!
**rake aborted!**
ruby.exe -S rspec ./spec/models/user_spec.rb **failed**
/ruby/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/rake_task.rb:156:in `run_task'
/ruby/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/rake_task.rb:124:in `block (2 levels) in initialize'
....
/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
ruby/gems/1.9.1/gems/rake-10.0.4/bin/rake:33:in `**<top (required)>**'
bin/rake:19:in `load'
bin/rake:19:in `<main>'
Tasks: TOP => spec:models
所以我的测试似乎通过了,但整体 rspec 结果失败了。如果我在没有spork 的情况下运行它,它就可以正常工作。所以我的(修改后的)问题是如何让它与 spork 一起使用?我需要为 spork 配置什么来解决我看到的问题吗?下面是 rspec 和 spork 文件。
这是“用户”的 rspec:
require_relative '../spec_helper'
require_relative '../../app/models/user'
describe User do
before :each do
@user = User.new login: 'TestUser', name: 'Test User 999'
end
it "login and name are required fields" do
@user.name.should_not be_nil
@user.login.should_not be_nil
end
it "can be instantiated" do
User.new.should be_an_instance_of(User)
end
end
这是 spec_helper.rb 的内容:
require 'spork'
Spork.prefork do
# 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'
# 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|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
那么使用 spork 运行时出现上述“最需要的”错误的根本原因是什么?我是否提供了确定根本原因所需的一切?
我查看了其他问题(What does mean <top (required)> in rspec?)(“<top (required)>”在 Ruby 堆栈跟踪中是什么意思?)但它们不是同一个问题。
TIA,
斯科特