【问题标题】:Rspec DB View Is EmptyRspec 数据库视图为空
【发布时间】:2021-12-01 06:28:18
【问题描述】:

我在我的 Rails 应用程序中创建了一个 LeadIndex 视图,该视图基于我的 Lead 模型。运行我的测试套件 (Rspec) 时,表(db 视图)被识别,但返回为空的 []LeadIndex::ActiveRecord_RelationLead 模型显示有对象存储在数据库中,但视图似乎没有接收到它。

例如:

Lead.all =>

[#<Lead:0x000000010a70a9a8
  id: 850,
  source: "Web",
  user_id: nil,
  created_at: Tue, 12 Oct 2021 18:48:32.505988000 UTC +00:00,
  updated_at: Tue, 12 Oct 2021 18:48:32.521121000 UTC +00:00,
  received_on: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
  assigned_on: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
  agency_id: 452,
  person_id: 882,
  age: 41,
  status: "prospecting",
  dnc: false,
  last_contact_time: nil>]
LeadIndex.all  => [] # actual

LeadIndex.all => #expected
[#<LeadIndex:0x000000015221ed48
 id: 850,
 source: "Web",
 user_id: nil,
 created_at: Tue, 12 Oct 2021 18:48:32.505988000 UTC +00:00,
 updated_at: Tue, 12 Oct 2021 18:48:32.521121000 UTC +00:00,
 received_on: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
 assigned_on: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
 agency_id: 452,
 person_id: 882,
 age: 0,
 status: "prospecting",
 dnc: false,
 last_contact_time: nil,
 scheduled_time: nil,
 name: "John Doe",
 person_state: "TX",
 number: "(555) 555-5555",
 num_outbound_dials: 3,
 last_activity: Tue, 12 Oct 2021 00:00:00.000000000 UTC +00:00,
 agent_name: "Tom Cruise",
 lang: "English",
 next_dial_time: "morning">
irb(main):002:0>]

在开发模式下一切正常。

【问题讨论】:

  • 你有点缺乏细节,请看How to Ask。特别是您需要以文本形式发布实际查询、表定义 (ddl)、示例数据和预期结果 - 无图像
  • 我已经更新了原帖。希望这可以帮助。似乎它必须是一个 Rspec 配置问题。因为它似乎在开发中运行良好。

标签: postgresql activerecord rspec rspec-rails ruby-on-rails-6


【解决方案1】:

嗯,我是个傻瓜。我想通了。在我的 SQL 查询中,我很难使用 where 子句来仅使用与我的开发数据库对象匹配的 ID 来显示结果。

因此,当测试环境为其对象组成随机 ID 时,WHERE 子句会过滤掉所有对象,因为没有与我的开发数据库 ID 匹配的确切数字的 ID..

一个大的d'oh!经验教训。

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    相关资源
    最近更新 更多