2014-01-18 48 views
0

工作,我有兩個以下型號:類method_1沒有通過測試,但它在軌道控制檯

class Project < ActiveRecord::Base 
has_many :tasks, inverse_of: :project 
attr_accessible :tasks_attributes 

def method_1 
    tasks.map |t| 
    t.name 
    end 
end 

class Task < ActiveRecord::Base 
belongs_to :project, inverse_of: :tasks 

attr_accessible :name 

validates :name, presence: true 
end 

我正在寫project_spec.rb測試。我測試協會和驗證,他們都很好。但不知何故,我無法通過method_1的測試。我改變了method_1代碼,只是爲了檢查。我得到的結果是[無],即tasks.first是零。我不知道我錯過了什麼。

def method_1 
    [tasks.first] 
end 

我的測試代碼,

... 
let(:project) do 
    build_stubbed(:project, :project_data) 
end 

describe "#method_1" do 
before do 
    project.stub_chain(:task, :name).and_return("task A") 
end 
subject {project.method_1} 
    it "should be the name of the task of the project" do 
    should eq ["task A"] 
    end 
end 
當我本地主機上運行

:3000,它有同樣的問題。但在rails控制檯中,method_1也可以正常工作。任何人都可以幫忙它困擾了我好幾天。

+0

不相關,但:'tasks.collect(&:name)' –

回答

0

這裏有一些評論,這可能有助於:

  • method_1依靠tasks,但你存根task(單數),所以它沒有任何效果。
  • 也許method_1作品在Rails的控制檯,因爲你必須在你的開發數據庫相關的項目任務,使用本地主機
  • 問題:3000可能是由於任何數量的事情,因爲你不通過直接使用模型方法網絡
相關問題