ruby-on-rails – 为什么升级到Rails 3.2.1导致多次Rspec测试失败?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 为什么升级到Rails 3.2.1导致多次Rspec测试失败?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的测试套件中的所有211规格都很好,直到我从rails 3.2升级到3.2.1版.现在我的规格中有197个失败了.这些错误中的大多数都有“错误的参数数(0为1)”错误,如下所述.

示例1:@H_301_3@

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

规格:@H_301_3@

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

失败:@H_301_3@

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行之前调用调试器,我得到:@H_301_3@

/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@H_301_3@

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,:uniqueness => true
end

规格:@H_301_3@

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

失败:@H_301_3@

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行是:@H_301_3@

user = User.find_by_email(params[:email])

此时检查参数显示所有内容(包括:电子邮件)都应该存在.@H_301_3@

对比例3@H_301_3@

FactoryGirl的这个规格很好:@H_301_3@

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

工厂定义如下:@H_301_3@

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通过.我不知道为什么会有所作为.@H_301_3@

但是,某些规格仍然没有通过.考虑以下模型(不使用FriendlyId),并规定3.2.1中的flunks:@H_301_3@

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@H_301_3@

1) DocumentLibrary can be shown on the company menu
 Failure/Error: dl = FactoryGirl.create(:document_library,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.@H_301_3@

这是我到目前为止所知道的@H_301_3@

>降级回到Rails 3.2.0使得所有的工作都可以
>升级到Edge Rails无法解决问题
> rspec宝石的运行边缘版本无法解决问题
>应用程序在开发模式下正常运行和预期.只有测试似乎受到影响.
>降级到ruby 1.9.2不能解决问题
>升级到ruby 1.9.3-p125不能解决问题
>从MysqLsqlite的测试环境不能解决问题
>将factory_girl_rails降级到1.6.0甚至1.5.0不能解决问题
>使用Factory.create(…)而不是Factory(…)不能解决问题
>使用FactoryGirl.define()和FactoryGirl.create()不能解决问题
>评论FriendlyId的东西使一些规格通过(见下文)
>运行db:test:prepare(或db:reset,db:migrate)无法解决问题
>将FactoryGirl的所有定义/创建更改为一致不会解决问题
>在新的rvm gemset下重新安装宝石(甚至完全重新安装rvm)不能解决问题
>在Test / Unit和FactoryGirl中重写这些测试不产生错误.所以FactoryGirl可能不是问题@H_301_3@

任何人都可以指出这个方向吗?还是提供故障排除建议?@H_301_3@

这是我的宝石:@H_301_3@

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核心库(没有测试宝石更改).@H_301_3@

解决方法

您可以尝试的第一件事(您没有按照列表)只是从测试数据库删除所有数据,并重新设置测试环境.由于某些原因,也许您的数据库已满测试数据,序列不工作和/或迁移尚未运行.

$rake db:test:prepare@H_301_3@

如果这不能解决问题,那么我首先让我的FactoryGirl定义和调用更加一致 – 不要粗鲁,但是我觉得在你发布的代码中看到3-4种不同的变体.它也使得难以从跟踪中解码隐藏错误消息 – 因为您与调用不一致.@H_301_3@

最佳实践定义:@H_301_3@

FactoryGirl.define do
  factory :document_library do
    sequence(:title) { |n| "Title #{n}" }
  end
end

最佳实践建设/创作:@H_301_3@

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的版本有关的问题.根据Changelog,Rails 3.2.1中的ActiveRecord模块没有任何更改.@H_301_3@

原文链接:https://www.f2er.com/ruby/272191.html

猜你在找的Ruby相关文章