5

我已经如下设置了 Rspec2 beta5 和 shoulda,以便在 rspec 模型测试中使用 shoulda 宏。

==================

2011 年 2 月 18 日更新

现在我们可以开箱即用地使用 shoulda-matchers。

只需gem shoulda-matchers在您的 Gemfile 中添加,而不是在 spec_helper 或任何 hack 中添加其他内容。它只是运行。

==================

宝石文件

group :test do
  gem "rspec", ">= 2.0.0.beta.4"
  gem "rspec-rails", ">= 2.0.0.beta.4"
  gem 'shoulda',          :git => 'git://github.com/bmaddy/
shoulda.git'
  gem "faker"
  gem "machinist"
  gem "pickle",           :git => 'git://github.com/codegram/
pickle.git'
  gem 'capybara',         :git => 'git://github.com/jnicklas/
capybara.git'
  gem 'database_cleaner', :git => 'git://github.com/bmabey/
database_cleaner.git'
  gem 'cucumber-rails',   :git => 'git://github.com/aslakhellesoy/
cucumber-rails.git'
end

spec_helper.rb

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

require 'shoulda'

Rspec.configure do |config|

规格/模型/outlet_spec.rb

require 'spec_helper'

describe Outlet do
  it { should validate_presence_of(:name) }
end

当我运行规范时,我收到以下错误。

[~/rails_apps/rails3_apps/automation (master)⚡] ➔ spec spec/models/
outlet_spec.rb
DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead.
(called from join at /home/millisami/.rvm/gems/ruby-1.9.1-p378%rails3/
bundler/gems/shoulda-87e75311f83548760114cd4188afa4f83fecdc22-master/
lib/shoulda/autoload_macros.rb:40)
F

1) Outlet
    Failure/Error: it { should validate_presence_of(:name) }
    undefined method `validate_presence_of' for
#<Rspec::Core::ExampleGroup::Nested_1:0xc4dc138 @__memoized={}>
    # ./spec/models/outlet_spec.rb:4:in `block (2 levels) in <top
(required)>'

Finished in 0.0399 seconds
1 example, 1 failures
[~/rails_apps/rails3_apps/automation (master)⚡] ➔

为什么是“未定义的方法”?应该加载了吗?

4

3 回答 3

7

使用 RSpec 2.0.0.beta.19

# Gemfile
group :test do
  gem "rspec", ">= 2.0.0.beta.19"
  gem "rspec-rails", ">= 2.0.0.beta.17"
  gem "shoulda"
end

# spec/spec_helper.rb
require 'rspec/rails'
require 'shoulda/integrations/rspec2' # Add this line

# In your specs....
it { should validate_presence_of(:name) }

Runningrake spec现在应该加载和运行规范,包括 RSpec 2 匹配器。

于 2010-08-11T07:19:01.770 回答
0

认为这与 rails3 中新的验证语法有关:

验证 :name, :presence => true

于 2010-04-12T21:25:08.157 回答
0

该方法应该是 validate s_presence_of。你错过了一个's'

于 2010-07-07T22:36:49.947 回答