测试字段在mongoid中键入模型

时间:2013-03-08 08:57:39

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

我在铁轨上使用红宝石。我也使用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

我如何测试一个字段的类型?提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

一个典型的例子是测试Stack类的功能。代替 将物品推出并弹出堆栈并检查尺寸,可能会更好 只需推送和弹出一些东西,看看如果你弹出一个空的堆栈,你会得到合适的 例外。当然,您要检查项目是否在最后输入,先退出 (LIFO)订单。

因此,在您的情况下,而不是测试您的字段name是什么类型的 使用名称测试你做什么