2017-08-09 114 views
0

我想测试我的应用程序,我不断收到以下错误:解决活动记录固定格式错误

Error: 
    ContractsControllerTest#test_should_get_show: 
    ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError 

Error: 
    ContractsControllerTest#test_should_get_show: 
    NoMethodError: undefined method `each' for nil:NilClass 

下面是我的合同控制器上的代码,我也有类似这样的代码上的所有控制器。

def index 
     @contract = Contract.all.paginate(page: params[:page], :per_page => 70) 
    end 


    def show 
     @contract = Contract.find(params[:id]) 

    end 



    def new 
     @contract = Contract.new 
    end 


    def create 
     @contract = Contract.new(located) 
     if @contract.save 
      flash[:success] = "A record has been successfully added" 
     redirect_to contracts_path 
     else 
      render 'new' 
     end 
    end 



    def located 
     params.require(:contract).permit(:contract_name, :contract_status, :services_rendered, :contract_value, :award_year) 
    end 


     # editing a record in the contract from cotract table 
    def edit 
     @contract = Contract.find(params[:id]) 
    end 





     def update 
     @contract = Contract.find(params[:id]) 
     if @contract.update_attributes(located) 
      flash[:success] = "Contract form updated" 
      redirect_to contracts_path 
     else 
      render'edit' 
     end 
     end 



    def destroy 
     Contract.find(params[:id]).destroy 
     flash[:success] = "A record has been successfully deleted" 
     redirect_to contracts_path 
    end 

下面是错误的完整跟踪时我运行命令导轨分贝:夹具:负载--trace

rails aborted! 
ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-  
5.0.5/lib/active_record/fixture_set/file.rb:72:in `validate' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixture_set/file.rb:49:in `raw_rows' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-  
5.0.5/lib/active_record/fixture_set/file.rb:37:in `config_row' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixture_set/file.rb:27:in `model_class' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:791:in `block (2 levels) in  
read_fixture_files' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixture_set/file.rb:15:in `open' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:790:in `block in read_fixture_files' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:789:in `each' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:789:in `each_with_object' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:789:in `read_fixture_files' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:598:in `initialize' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:529:in `new' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:529:in `block (2 levels) in  
create_fixtures' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:526:in `map' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:526:in `block in create_fixtures' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/connection_adapters/postgresql/referential_integrity.rb: 22:in `disable_referential_integrity' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/fixtures.rb:523:in `create_fixtures' 
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 
5.0.5/lib/active_record/railties/databases.rake:207:in `block (3 levels) in 
<top  (required)>' 
/usr/local/rvm/gems/ruby-2.3.0/gems/railties- 
5.0.5/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task' 
/usr/local/rvm/gems/ruby-2.3.0/gems/railties- 
5.0.5/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task' 
/usr/local/rvm/gems/ruby-2.3.0/gems/railties- 
5.0.5/lib/rails/commands/commands_tasks.rb:51:in `run_command!' 
/usr/local/rvm/gems/ruby-2.3.0/gems/railties- 
5.0.5/lib/rails/commands.rb:18:in `<top (required)>' 
/home/ubuntu/workspace/final_project/bin/rails:9:in `require' 
/home/ubuntu/workspace/final_project/bin/rails:9:in `<top (required)>' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring- 
2.0.2/lib/spring/client/rails.rb:28:in `load' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring- 
2.0.2/lib/spring/client/rails.rb:28:in `call' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring- 
2.0.2/lib/spring/client/command.rb:7:in `call' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:in  
`run' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/bin/spring:49:in `<top 
(required)>' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-  
2.0.2/lib/spring/binstub.rb:31:in `load' 
/usr/local/rvm/gems/ruby-2.3.0/gems/spring- 
2.0.2/lib/spring/binstub.rb:31:in`<top (required)>' 
/home/ubuntu/workspace/final_project/bin/spring:15:in `require' 
/home/ubuntu/workspace/final_project/bin/spring:15:in `<top (required)>' 
bin/rails:3:in `load' 
bin/rails:3:in `<main>' 
Tasks: TOP => db:fixtures:load 
(See full trace by running task with --trace) 

我的申请containts 12个控制器,在每个出现相同错误控制器。另外,我在我的模式中有10个表,其中一些表包含彼此的引用/关系。

我真的不知道什么是错,现在我已经为这个问题挣扎了四天。如果有人能够帮助我解决任何可能帮助我追踪错误来源的代码或信息,我将不胜感激。如果您能够提供有关我应该将代码放在哪里以便跟踪和解决此错误的信息,我也不胜感激。

谢谢

+0

我的回答是否帮助您解决问题?在StackOverflow上,您应该对收到的答案提供反馈,尤其是在他们帮助您的时候。如果答案解决了您的问题,那么您应该将答案标记为已接受,这将有助于其他具有相同问题的人找到解决方案。 – DRSE

+0

另外,我注意到你上周发布了这个相同的问题三次,每个帖子都有细微的不同细节(这里是https://stackoverflow.com/questions/45580331/active-record-fixture-format-error,这里是https:/ /stackoverflow.com/questions/45473138/active-record-fixture-format-error),并没有跟进他们中的任何一个。你不应该这样做;相反,您应该更新原始帖子并提供更多详细信息。这将把所有的问题信息保存在一个地方,以帮助你和那些试图提供帮助的人。忽视他人的建议和努力,然后重新发布是不礼貌的。 – DRSE

+0

DRSE我很抱歉。事实上,我急于提交一个项目,但仍然是错误,我继续得到错误,这就是为什么。 – fred

回答

1

其中一个夹具文件在测试/灯具中的格式不正确。这个问题似乎与您的控制器或测试无关,因为它发生在每一个人身上。所有灯具在每次测试之前都会加载到测试数据库中,即使其中一些未用于当前测试。因此,如果这些文件中有一个出现错误,那么每次测试都会产生错误。

根据line from the activerecord source将错误提示到您提供的堆栈跟踪顶部,ActiveRecord预计YAML夹具文件中的每个条目都是引用散列的键。

灯具中的每个条目应代表一个模型实例。顶级密钥用作引用该实例的名称(在生成新模型的灯具模板时,导轨通常将它们命名为onetwo)。这里有一个YAML夹具的例子,它有一个好的入口和几个不同类型的坏的入口。

# This will produce a hash associated to key :hash_entry. 
# This is the correct type of entry, all others that follow are incorrect for rails fixtures. 
hash_entry: 
    key1: value1 
    key2: value2 

# This will produce the string "not a hash" associated to key :string_entry 
string_entry: not a hash 

# This will produce the array ["also", "not","a","hash"] associated to key :array_entry 
array_entry: 
    - also 
    - not 
    - a 
    - hash 

# This will produce nil associated to key :nil_entry 
nil_entry: 

你需要检查你的灯具测试/夹具文件和寻找任何有坏的格式,如上所述。这是一个rake任务,可帮助您确定哪些文件和条目需要更正。首先运行rails g task fixtures check_format,并将此代码放入在lib/tasks/fixtures.rake处生成的rake文件中。

namespace :fixtures do 
    desc "Looks for bad fixture files" 
    task check_format: :environment do 
    fixtures_dir = Rails.root.join("test", "fixtures") 
    fixture_files = Pathname.glob("#{fixtures_dir}/**/*.yml") 

    fixture_files.each do |file| 
     fixture = YAML.load(IO.read(file)) 

     fixture.each_pair do |name, entry| 
      puts "Bad fixture entry #{name}: #{entry.inspect} in fixture #{file}" unless entry.is_a? Hash 
     end 
    end 
    end 
end 

然后运行rails fixtures:check_format和有问题的文件和项目将被打印出来在命令行上为您找到和正确的。