我测试套件中的所有211规格都正常通过......直到我从rails 3.2升级到rails 3.2.1。现在197我的规格失败了。大多数错误都有“错误的参数数量(0表示1)”错误,如下所述。
示例#1:
class DocumentLibrary < ActiveRecord::Base
extend FriendlyId
friendly_id :title, :use => :slugged
has_many :shelves, :dependent => :destroy
has_many :documents, :through => :shelves
validates :title, :presence => true, :uniqueness => true
default_scope :order => :title
end
规格:
it "can be shown on the company menu" do
dl = FactoryGirl.create(:document_library, :title => 'Test', :menu => false, :company => true)
dl.should be_valid
end
失败:
1) DocumentLibrary can be shown on the company menu
Failure/Error: dl = FactoryGirl.create(:document_library, title: 'Test', menu: false, company: true)
ArgumentError:
wrong number of arguments (0 for 1)
# ./spec/models/document_library_spec.rb:6:in `block (2 levels) in <top (required)>'
如果我在FactoryGirl.create行之前调用调试器,我得到:
/Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:252:
(rdb:1) c
/Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.1/lib/active_support/core_ext/module/remove_method.rb:4: `' (NilClass)
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:249:in `set_it_up'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:200:in `subclass'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:187:in `describe'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/dsl.rb:18:in `describe'
from /Users/Jason/code/rails/teamsite/spec/models/document_library_spec.rb:4:in `<top (required)>'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `block in load_spec_files'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `map'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load_spec_files'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in `run'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'
/Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.1/lib/active_support/core_ext/module/remove_method.rb:4:
示例#2
class Album < ActiveRecord::Base
belongs_to :photo_library
validates :title, :presence => true
validates :title, :uniqueness => {scope: :photo_library_id}
end
class PhotoLibrary < ActiveRecord::Base
default_scope :order => :title
has_many :albums, :dependent => :destroy
validates :title, :presence => true
validates :title, :uniqueness => true
end
规格:
before :each do
@photo_library = FactoryGirl.create(:photo_library, title: 'Products')
login_admin
end
it "destroys an album" do
3.times { FactoryGirl.create(:album, photo_library: @photo_library) }
visit photo_library_path(@photo_library)
find("h3").click_link 'Delete'
find("#notice").should have_content("Album deleted successfully")
Album.count.should eq 2
end
失败:
1) Albums destroys an album
Failure/Error: login_admin
ArgumentError:
wrong number of arguments (0 for 1)
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `click_button'
# ./spec/requests/albums_spec.rb:7:in `block (2 levels) in <top (required)>'
我的sessions_controller中的第8行是:
user = User.find_by_email(params[:email])
此时检查参数显示所有内容(包括:电子邮件)都应该存在。
对比示例#3
FactoryGirl的这个规范很好:
it "is unique within a library" do
pl = FactoryGirl.create(:photo_library, title: 'Products')
pl2 = FactoryGirl.create(:photo_library, title: 'Competitors')
a = FactoryGirl.create(:album, title: 'Gold Series', photo_library: pl)
na = Album.create(photo_library_id: pl.id, title: 'Gold Series')
na.should_not be_valid
na.title = 'Cyclone'
na.should be_valid
na = Album.create(photo_library_id: pl2.id, title: 'Gold Series')
na.should be_valid
end
工厂的定义如下:
factory :document_library do
sequence(:title) { |n| "Library Title#{n}" }
end
factory :photo_library do
sequence(:title) { |n| "Photo Library Title#{n}" }
end
factory :album do
sequence(:title) {|n| "Album#{n}"}
end
如果我从DocumentLibrary模型中注释掉FriendlyId行,则示例#1通过。我不知道为什么会有所作为。
但是,有些规格仍未通过。考虑以下模型(不使用FriendlyId)和3.2.1中的规范:
class DrawingLibrary < ActiveRecord::Base
validates :title, :presence => true
default_scope order: :title
has_many :drawings, :dependent => :destroy
end
it "displays in alpha order" do
FactoryGirl.create(:drawing_library, title: 'C')
FactoryGirl.create(:drawing_library, title: 'B')
FactoryGirl.create(:drawing_library, title: 'A')
ts = ''
DrawingLibrary.all.each do |draw_lib|
ts += draw_lib.title
end
ts.should eq 'ABC'
end
1) DrawingLibrary displays in alpha order
Failure/Error: DrawingLibrary.all.each do |draw_lib|
ArgumentError:
wrong number of arguments (0 for 1)
# ./spec/models/drawing_library_spec.rb:19:in `block (2 levels) in <top (required)>'
结果:rspec spec / models / document_library_spec.rb --backtrace
1) DocumentLibrary can be shown on the company menu
Failure/Error: dl = FactoryGirl.create(:document_library, title: 'Test', menu: false, company: true)
ArgumentError:
wrong number of arguments (0 for 1)
# ./spec/models/document_library_spec.rb:6:in `block (2 levels) in <top (required)>'
我正在使用rvm和ruby 1.9.3,Rubygems是1.8.16。 Factory Girl是2.6.0,factory_girl_rails是1.7.0。 rspec-rails是2.8.1。
这是我目前所知道的:
有人能指出我的方向吗?或提供故障排除建议?
这是我的Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.2.1'
gem 'mysql2'
gem 'dynamic_form'
gem 'friendly_id'
group :assets do
gem 'sass-rails', " ~> 3.2.3"
gem 'coffee-rails', "~> 3.2.1"
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'therubyracer'
gem 'bcrypt-ruby'
gem 'capistrano'
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
group :test, :development do
gem 'rspec-rails'
gem 'launchy'
gem 'ruby-debug19'
gem 'database_cleaner'
gem 'capybara-webkit'
gem 'spork', '~> 0.9.0.rc'
gem 'guard-spork'
end
group :development do
gem 'fuubar'
gem 'powder'
end
group :test do
gem 'turn', :require => false
gem 'capybara'
gem 'factory_girl_rails'
gem 'guard-rspec'
end
升级到Rails 3.2.1后,Gemfile.lock的唯一区别在于Rails核心库(没有更改测试宝石)。
答案 0 :(得分:3)
您可以尝试的第一件事(您没有根据列表)只是从测试数据库中删除所有数据并“重置”您的测试环境。出于某种原因,您的数据库可能充满了测试数据,序列无法运行和/或尚未运行迁移。
$ rake db:test:prepare
如果这不能解决问题,那么我首先要使我的FactoryGirl定义和调用更加一致 - 不要粗鲁,但我想我在你发布的代码中看到了3-4种不同的变化。它还使得从跟踪中解密神秘错误消息变得更加困难 - 因为您与调用不一致。
最佳做法定义:
FactoryGirl.define do
factory :document_library do
sequence(:title) { |n| "Title #{n}" }
end
end
建立/创建最佳实践:
document_library = FactoryGirl.create(:document_library, {
title: 'A',
visible: false
})
# or
document_library = FactoryGirl.build(:document_library)
我知道你写的它在Rails 3.2.0中运行,并且一切都是Rails 3.2.1错误,但是可能只有3.2.1 MIXED与你正在使用RSpec和FactoryGirls的版本有关。根据{{3}},Rails 3.2.1中的ActiveRecord模块没有任何变化。
答案 1 :(得分:2)
这可能不是根本原因,但由于您的错误提示FactoryGirl,我首先将所有工厂(及其创建)转换为另一种(新的/推荐的(?))语法,即FactoryGirl.define do ...
和doclib = FactoryGirl.create(:document_library ...
。可能是Factory
在运行规范时会有所不同(与控制台中的规格不同)。
答案 2 :(得分:0)
它可能是模型中过时的语法。 取代
default_scope order: :title
带
default_scope :order => :title
如果我从DocumentLibrary模型中注释掉FriendlyId行,则上面的规范通过。我不知道为什么会有所作为
通过此修改,default_scope order: :title
成为模型的最后一行
答案 3 :(得分:0)
在您提到的第一个规范中,您可以将以下内容添加到控制器文件中:
def create
puts params.inspect
user = User.find_by_email(params[:email])
end
我认为你没有从params获得任何信息。由于这是规范中的login_admin
函数,因此您可能正在使用before(:each)
或let
- 块来登录您的大多数规范,因此这可能是问题的根源。
现在注意到它:)
答案 4 :(得分:0)
也许你应该尝试创建一个新的gemset,以防某些宝石相互干扰。
$ rvm use 1.9.3@{unique-gemset-name-here} --create
清洁宝石$ bundle
$ rake spec
正如我过去两天一直关注这一点,我开始考虑你是否应该暂时放弃3.2.1升级(鉴于这是一个高优先级项目)。为您的客户和/或用户获取价值,而不是花时间在3.2.1上。有人会在某个时候解决这个问题然后你可以继续进行升级。