2

我在 rails 2 应用程序中使用了 thinkbot-should gem。模型是

class Zone < ActiveRecord::Base
  belongs_to :organization
  has_many :volunteer_shifts

  validates_presence_of :organization_id
  validates_presence_of :name
end

$cat spec/models/zone_spec.rb 

require 'spec_helper'
describe Zone do
  context "should" do
    it { should have_db_column(:organization_id) }#, :decimal}
    it { should have_db_column(:name) }
    it { should have_db_column(:description) }

    it { should belong_to(:organization) }
    it { should have_many(:volunteer_shifts) }

    it { should validate_presence_of(:organization_id) }
    it { should validate_presence_of(:name) }
  end
end

但规范没有说

'Zone should should require organization_id to be set' FAILED
Expected errors to include "%{attribute}: can't be blank" when organization_id is set to nil, got errors: organization_id Organization: can't be blank (nil)name Name: can't be blank (nil)

所有验证匹配器都失败。参考站点是http://rubydoc.info/gems/thoughtbot-shoulda/2.11.1/frames

$ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [i686-linux]
$rails -v
Rails 2.3.16
$cat spec/spec_helper.rb | grep shoulda
require 'shoulda/rails'

group :test do
  gem 'rspec', '1.3.2', :require => false
  gem 'rspec-rails', '1.3.4', :require => false
  gem 'thoughtbot-shoulda', "2.11.1"
end

$bundle exec script/console 
NOTE: Gem.source_index is deprecated, use Specification. It will be removed on or after   2011-11-01.
Gem.source_index called from   ./script/../config/../vendor/rails/railties/lib/rails/gem_dependency.rb:21.
Loading development environment (Rails 2.3.16)
NOTE: Gem.source_index is deprecated, use Specification. It will be removed on or after 2011-11-01.
Gem.source_index called from /home/prasad/projects/app/config/../vendor/rails/railties/lib/rails/gem_dependency.rb:21.
>> Zone
SQL (0.3ms)   SET NAMES 'utf8'
SQL (0.2ms)   SET SQL_AUTO_IS_NULL=0
SQL (2.4ms)   SHOW TABLES
Zone Columns (1.6ms)   SHOW FIELDS FROM `zones`
=> Zone(id: integer, organization_id: integer, name: string, description: text, created_at: datetime, updated_at: datetime)

Gemfile.lock

GEM
 remote: http://rubygems.org/
 specs:
Ascii85 (1.0.2)
actionmailer (2.3.16)
  actionpack (= 2.3.16)
actionpack (2.3.16)
  activesupport (= 2.3.16)
  rack (~> 1.1.0)
active_utils (1.0.3)
  activesupport (>= 2.3.11)
  i18n
activerecord (2.3.16)
  activesupport (= 2.3.16)
activeresource (2.3.16)
  activesupport (= 2.3.16)
activesupport (2.3.16)
acts_as_xlsx (1.0.5)
  activerecord (>= 2.3.9)
  axlsx (>= 1.0.13)
  i18n (>= 0.4.1)
addressable (2.3.3)
airbrake (3.1.8)
  activesupport
  builder
  json
authorize-net (1.5.2)
  nokogiri (~> 1.4)
axlsx (1.3.5)
  htmlentities (~> 4.3.1)
  nokogiri (>= 1.4.1)
  rubyzip (>= 0.9.5)
barby (0.5.1)
builder (3.2.0)
calendar_date_select (1.16.1)
capistrano (2.9.0)
  highline
  net-scp (>= 1.0.0)
  net-sftp (>= 2.0.0)
  net-ssh (>= 2.0.14)
  net-ssh-gateway (>= 1.1.0)
capistrano-ext (1.2.1)
  capistrano (>= 1.0.0)
capybara (1.1.4)
  mime-types (>= 1.16)
  nokogiri (>= 1.3.3)
  rack (>= 1.0.0)
  rack-test (>= 0.5.4)
  selenium-webdriver (~> 2.0)
  xpath (~> 0.1.4)
capybara-webkit (0.12.1)
  capybara (>= 1.0.0, < 1.2)
  json
childprocess (0.3.9)
  ffi (~> 1.0, >= 1.0.11)
cocaine (0.4.2)
commonjs (0.2.6)
crack (0.1.8)
cucumber (1.1.0)
  builder (>= 2.1.2)
  diff-lcs (>= 1.1.2)
  gherkin (~> 2.5.0)
  json (>= 1.4.6)
  term-ansicolor (>= 1.0.6)
cucumber-rails (0.3.2)
  cucumber (>= 0.8.0)
daemon-spawn (0.3.0)
daemons (1.0.10)
delayed_job (2.0.7)
  activesupport (~> 2.0)
  daemons (= 1.0.10)
diff-lcs (1.2.1)
factory_girl (2.6.4)
  activesupport (>= 2.3.9)
faker (1.1.2)
  i18n (~> 0.5)
fakeweb (1.3.0)
fastercsv (1.5.3)
ffi (1.4.0)
gherkin (2.5.4)
  json (>= 1.4.6)
graticule (0.2.12)
  activesupport
highline (1.6.16)
hodel_3000_compliant_logger (0.1.1)
htmlentities (4.3.1)
httparty (0.6.1)
  crack (= 0.1.8)
i18n (0.5.0)
json (1.7.7)
launchy (2.2.0)
  addressable (~> 2.3)
less (2.1.0)
  commonjs (~> 0.2.0)
  therubyracer (~> 0.10.0)
libv8 (3.3.10.4)
lockfile (1.4.3)
macaddr (1.6.1)
  systemu (~> 2.5.0)
mail (2.4.1)
  i18n (>= 0.4.0)
  mime-types (~> 1.16)
  treetop (~> 1.4.8)
mechanize (1.0.0)
  nokogiri (>= 1.2.1)
mime-types (1.21)
multi_json (1.7.0)
multipart-post (1.1.0)
mysql (2.8.1)
net-scp (1.1.0)
  net-ssh (>= 2.6.5)
net-sftp (2.1.1)
  net-ssh (>= 2.6.5)
net-ssh (2.6.6)
net-ssh-gateway (1.2.0)
  net-ssh (>= 2.6.5)
newrelic_rpm (3.5.8.72)
nokogiri (1.4.7)
oink (0.1.2)
  hodel_3000_compliant_logger
paperclip (2.8.0)
  activerecord (>= 2.3.0)
  activesupport (>= 2.3.2)
  cocaine (>= 0.0.2)
  mime-types
pdf-reader (0.8.5)
  Ascii85 (>= 0.9)
pipejump (0.4.4)
polyglot (0.3.3)
postageapp (1.0.14)
  json
rack-test (0.6.2)
  rack (>= 1.0)
rails (2.3.16)
  actionmailer (= 2.3.16)
  actionpack (= 2.3.16)
  activerecord (= 2.3.16)
  activeresource (= 2.3.16)
  activesupport (= 2.3.16)
  rake (>= 0.8.3)
rake (0.8.7)
rcov (1.0.0)
rdoc (2.4.3)
rmagick (2.12.2)
rspec (1.3.2)
rspec-rails (1.2.9)
  rack (>= 1.0.0)
  rspec (>= 1.2.9)
rubyzip (0.9.7)
sanitize (2.0.0)
  nokogiri (~> 1.4.4)
selenium-webdriver (2.31.0)
  childprocess (>= 0.2.5)
  multi_json (~> 1.0)
  rubyzip
  websocket (~> 1.0.4)
steak (1.1.0)
  rspec (>= 1.3)
system_timer (1.0)
systemu (2.5.2)
term-ansicolor (1.1.1)
therubyracer (0.10.2)
  libv8 (~> 3.3.10)
thoughtbot-shoulda (2.11.1)
timecop (0.6.1)
treetop (1.4.10)
  polyglot
  polyglot (>= 0.3.1)
uuid (2.3.6)
  macaddr (~> 1.0)
vpim (0.695)
webrat (0.5.3)
  nokogiri (>= 1.2.0)
  rack (>= 1.0)
websocket (1.0.7)
wicked_pdf (0.7.9)
xpath (0.1.4)
  nokogiri (~> 1.3)
4

1 回答 1

1

我重新创建了您的代码,一切正常(并且规范通过了)。你有require 'shoulda'你的spec_helper,对吧?我唯一能想到的另一件事是你可能有一段时间没有跑步db:migrate了。另外,你确定上ruby 1.8.7吗?很难想到你所拥有的和我在本地拥有的有什么不同。

于 2013-04-04T10:35:29.270 回答