【问题标题】:Rails - How to test enum PostgreSQL data-type with rspec?Rails - 如何使用 rspec 测试枚举 PostgreSQL 数据类型?
【发布时间】:2016-11-22 16:05:29
【问题描述】:

我正在使用 shoulda-matchers 来测试枚举。

describe 'enum' do
  it do
    should define_enum_for(:gender).
    with([:male, :female, :others])
  end
end

最近我把DB中的column改为enum数据类型,如图here

我当前在用户模型中的枚举定义。

  enum gender: {male:'male', female:'female', others:'others'}

之后,我的测试(显然)失败并显示以下消息。

 1) User enum should define :gender as an enum with [:male, :female, :others] and store the value in a column with an integer type

如何使用 rspec 进行测试?

【问题讨论】:

  • 你能告诉我们你的枚举是如何在模型中定义的吗?
  • 我用模型@Vimsha 的枚举定义更新了我的问题

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


【解决方案1】:

因为我最近遇到了同样的问题而发帖。

您可以在 RSpec 中明确指定列类型和值:

it "defines an enum" do
  expect(field).to define_enum_for(:column_name)
                     .backed_by_column_of_type(:enum)
                     .with_values(male: "male", female: "female", others: "others")
end

注意:

  • :enum 列类型,如果您定义了相应的 postgres 枚举;否则你也可以在这里使用:string。对于 :enum 类型,RSpec 期望值 :enum 而不是枚举的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多