2013-03-08 42 views
1

我在軌道上使用紅寶石。我也使用minitest框架進行測試,並使用mongoid作爲數據庫。我想寫一個模型測試。我的模型如下:測試字段在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 

如何測試一個字段的類型?提前致謝。

+0

也許你需要像'Identity.new.name.is_a? String'? – 2013-03-08 09:00:55

+0

太基地你不使用rspec:https://github.com/evansagge/mongoid-rspec – apneadiving 2013-03-08 09:19:18

回答

0

只是一個提示 - 而不是一個回答您的實際問題,它通常不是代碼 明智的測試實現細節,因爲它更容易 改變,而如果通過測試,認爲系統的驗證,它是不是那麼重要 如何它是如何實現的,但是它做什麼,例如。 它的行爲如何

一個典型的例子是測試Stack類的功能。代替 推送物品並將其彈出堆棧並檢查大小,最好是僅推送和彈出東西,並看到如果彈出空堆棧,則會得到適當的 異常。當然,你想檢查的項目是在最後一個返回,首先出 (LIFO)的順序。

所以,你的情況,而不是測試什麼樣的鍵入場name的是,相當 測試你的名稱。

相關問題