2012-03-13 91 views
1

我未能谷歌有關此任何事情,所以我把它帶到這裏。 我在我的模型上覆蓋了to_param,將它與id連接起來構成一個更漂亮,更安全的URL。我不知道這應該在我的測試中。這似乎是把它放到路由測試中忽略了這一點。我認爲,這將走在模特的規格,雖然:Rspec測試to_param覆蓋

#my model 
class MyModel < ActiveRecord::Base 
    def to_param 
    [id,an_attr.parameterize].join("-") 
    end 
end 
#my model spec 
describe MyModel do 
    mymodel = MyModel.create!(:an_attr => "test attr") 
    mymodel.to_param should == [mymodel.id,"test_attr"].join("-") 
end 

我得到的是:

Failure/Error: mymodel.to_param should == [mymodel.id,"test_attr"].join("-") 
    expected: "1-test_atttr" 
     got: #<MyModel id: nil, an_attr: nil, created_at: nil, updated_at: nil> (using ==) 

Model實例......我必須在這裏失去了一些東西很明顯 - - 也許它剛剛晚了--- 洞察力非常感謝。

回答

2

看起來你錯過了to_param之後的一段時間。

mymodel.to_param.should == [mymodel.id,"test_attr"].join("-")

+0

doh!...是的。那肯定是......它是否是測試URL段落的適當位置/方式?看起來你不想在路由器上這樣做,因爲你必須存根模型,對吧? – 2012-03-13 18:52:14