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

Ruby on rails 解决活动记录夹具格式错误,ruby-on-rails,Ruby On Rails,我正在尝试测试我的应用程序,但不断出现以下错误: Error: ContractsControllerTest#test_should_get_show: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError Error: ContractsControllerTest#test_should_get_show: NoMethodError: undefined method

我正在尝试测试我的应用程序,但不断出现以下错误:

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中止!
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\u record/fixture\u set/file.rb:49:在“原始行”中
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active\u record/fixture\u set/file.rb:37:在'config\u行'
/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层)in`
读取\u夹具\u文件'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:15:在“打开”中
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active\u record/fixtures.rb:790:in`block in read\u fixture\u 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\u record/fixtures.rb:789:in“read\u fixture\u 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层)in`
创建_装置'
/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层)in`
'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/rake\u proxy.rb:14:in`block in run\u rake\u task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/rake\u proxy.rb:11:in'run\u rake\u task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/commands\u tasks.rb:51:in'run\u command!'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands.rb:18:in`'
/home/ubuntu/workspace/final_project/bin/rails:9:in'require'
/home/ubuntu/workspace/final_project/bin/rails:9:in`'
/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
`跑
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/bin/spring:49:in`'
/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`'
/home/ubuntu/workspace/final_project/bin/spring:15:in'require'
/home/ubuntu/workspace/final_project/bin/spring:15:in`'
箱子/轨道:3:在“装载”中
箱子/轨道:3:in`'
任务:TOP=>db:fixture:load
(通过使用--trace运行任务查看完整跟踪)
我的应用程序包含12个控制器,每个控制器中都会发生相同的错误。此外,我的模式中有10个表,其中一些表包含相互的引用/关系

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


谢谢

测试/装置中的一个装置文件格式不正确。问题似乎不在控制器或测试上,因为它们中的每一个都有问题。在每次测试之前,所有夹具都会加载到测试数据库中,即使其中一些夹具没有用于当前测试。因此,如果其中一个文件中存在错误,则每次测试都会引发该错误

根据在您提供的堆栈跟踪顶部抛出错误的消息,
ActiveRecord
希望YAML fixture文件中的每个条目都是引用散列的键

夹具中的每个条目都应表示一个模型实例。顶级键用作引用该实例的名称(在为新模型生成夹具模板时,rails通常将它们命名为
one
two
)。这里是一个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/fixture中的fixture文件,并查找具有上述错误格式的文件。下面是一个rake任务,它将帮助您确定哪些文件和条目需要更正。首先运行rails g task fixtures check_format,并将此代码放入rake文件中,该文件在
lib/tasks/fixtures.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
,有问题的文件和条目将在命令l上打印出来
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