解决活动记录夹具格式错误

时间:2017-08-09 10:39:28

标签: ruby-on-rails

我正在尝试测试我的应用程序,并且我不断收到以下错误:

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

以下是运行命令rails db:fixtures:load --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)

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

我真的不知道出了什么问题,我现在已经在这个问题上苦苦挣扎了四天。如果有人能帮助我查找任何可以帮助我追踪错误来源的代码或信息,我将不胜感激。如果您能提供有关我应该在何处放置代码以查找并解决此错误的确切信息,我将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

test / fixtures中的一个fixture文件格式错误。问题似乎与您的控制器或测试无关,因为它们正在发生在每一个控制器或测试中。在每次测试之前,所有灯具都会加载到测试数据库中,即使某些灯具不用于当前测试。因此,如果其中一个文件出现错误,则每次测试都会引发错误。

根据您提供的堆栈跟踪顶部的错误line from the activerecord sourceActiveRecord期望YAML fixture文件中的每个条目都是引用的键哈希。

灯具中的每个条目都应代表一个模型实例。顶级键用作引用该实例的名称(rails在生成新模型的fixture模板时通常将它们命名为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: 

您需要检查test / fixtures中的fixtures文件,并查找具有如上所述的错误格式的任何文件。这是一个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,将在命令行打印出有问题的文件和条目,供您查找和更正。