【问题标题】:testing field type a model in mongoid测试字段在 mongoid 中键入模型
【发布时间】:2013-03-08 08:57:38
【问题描述】:

我在 Rails 上使用 ruby​​。我也使用 minitest 框架进行测试,使用 mongoid 进行数据库。我想写一个模型测试。我的模型如下:

class Identity
  include Mongoid::Document
  include OmniAuth::Identity::Models::Mongoid

  field :name
  field :email
  field :password_digest

  validates :name, uniqueness: true
end

模型测试是:

describe Identity do
  it "must include OmniAuth::Identity::Models::Mongoid" do
    Identity.must_include OmniAuth::Identity::Models::Mongoid
  end

  it "should have name" do
    Identity.new.must_respond_to :name
  end

  it "should have email" do
    Identity.new.must_respond_to :email
  end

  it "should have password_digest" do
    Identity.new.must_respond_to :password_digest
  end

   it "should type of String" do
    Identity.new.name.type.must_equal "String"
  end
end

我的问题是关于测试字段的类型

it "should type of String" do
  Identity.new.name.type.must_equal "String"
end

如何测试字段的类型?提前致谢。

【问题讨论】:

标签: ruby-on-rails ruby testing model mongoid


【解决方案1】:

只是一个提示 - 虽然不是您实际问题的答案,但通常不是这样 明智的测试代码的实现细节,因为它更有可能 改变,如果你认为通过测试来验证系统,它并不重要 如何它是如何实现的,但 它做了什么,例如。 它的行为方式

一个典型的例子是测试 Stack 类的功能。代替 从堆栈中推送和弹出项目并检查大小,这可能会更好 只是推送和弹出东西,看看如果你弹出一个空堆栈,你会得到适当的 例外。当然,您要检查物品是否后进先出 (LIFO) 顺序。

因此,在您的情况下,与其测试您的字段 name 的类型,不如测试 用这个名字测试你做什么

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2021-08-06
    • 2017-03-19
    • 2018-07-05
    相关资源
    最近更新 更多