2014-09-21 35 views
0

當試圖編寫名爲Test的模型的規格時,我與Test:Module發生衝突。如何使用rspec編寫名爲Test的模型的規格

這裏是我的代碼:

require 'rails_helper' 

RSpec.describe Test, :type => :model do 

    before { @test = Test.new(title: "Sample Test", description: "Description of sample test") } 
    subject { @test } 

    it { should respond_to(:title)} 
    it { should respond_to(:description)} 

end 

而我得到的錯誤

undefined method `new' for Test:Module 

如果有人知道如何正確地寫這個,謝謝。

+1

我想,如果你走這條路,這可能不是你最後的障礙。我嘗試通過運行'rails generate scaffold Test title:string description:string'來重現它,但Rails用'The name'Test'已經在您的應用程序中使用或由Ruby on Rails保留的消息拒絕它。請選擇一個替代品並再次運行此生成器.' – Amir 2014-09-21 08:10:17

+0

謝謝,我將使用替代名稱,如「Teste」 – 2014-09-21 11:32:13

+0

使用「測試」作爲模型名稱可能是一個壞主意。它可能與Ruby或Rails中的保留字發生衝突。 – 2014-09-21 08:43:16

回答

0

通過引用它作爲:: Test,您可以明確引用外部名稱空間的Test常量。它另外匹配一個Rspec相關的Test模塊。