【问题标题】:Rails unexpected token tCOMMARails 意外令牌 tCOMMA
【发布时间】:2017-05-02 02:13:17
【问题描述】:

我在 RSpec 中创建了这个对象。我在第二次}, 收到错误unexpected token tCOMMA 知道为什么会收到此错误吗?

   let(:test) {
     { abc:
       [{ bde:
         { attribute1: 170.0,
           attribute2: '2016-12-14',
           attribute3: 'self entered',
           attribute4: 'high',
           attribute5:
             { attribute6: 'low',
               attribute7: false,
               attribute8: nil,
               attribute9: 129,
               attribute0: 'mg/dL'},
             { attribute11: 'moderate',
               attribute12: false,
               attribute13: 130,
               attribute14: 159,
               attribute15: 'mg/dL' }, # getting error unexpected token tCOMMA
             { attribute16: 'high',
               attribute17: true,
               attribute18: 160,
               attribute19: nil,
               attribute20: 'mg/dL' }
         }
       }]
     }}

【问题讨论】:

  • 如果你的代码格式正确,你会看到attribute5应该是一个数组

标签: ruby-on-rails ruby-on-rails-4 rspec rspec-rails


【解决方案1】:

看起来您的代码中的attribute5 是一个数组,而不是一个散列。试试这个代码:

   let(:test) {
            {
              abc: [{
               bde: {
                 attribute1: 170.0,
                 attribute2: '2016-12-14',
                 attribute3: 'self entered',
                 attribute4: 'high',
                 attribute5: [{
                     attribute6: 'low',
                     attribute7: false,
                     attribute8: nil,
                     attribute9: 129,
                     attribute0: 'mg/dL'
                 },
                 {
                     attribute11: 'moderate',
                     attribute12: false,
                     attribute13: 130,
                     attribute14: 159,
                     attribute15: 'mg/dL'
                 },
                 {
                     attribute16: 'high',
                     attribute17: true,
                     attribute18: 160,
                     attribute19: nil,
                     attribute20: 'mg/dL'
                 }]
              }
            }]
          }
        }

【讨论】:

  • 谢谢。我试过了,但是当我运行 Rspec 时出现错误undefined method key?对于 #<0x007fa4946d9260>
猜你喜欢
  • 2018-02-11
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 2014-11-29
  • 2018-11-19
  • 2017-04-17
  • 2019-05-26
相关资源
最近更新 更多