2010-11-01 38 views
0

使用資源路由,您可以執行諸如url_for(@apple)之類的操作來獲取特定資源「show」方法的url。但是,在測試中,使用Mocha模擬我的對象時,我無法生成適當的資源路徑。在Rails中使用Mocha測試form_for路徑生成

E.g.考慮如何url_for路線的這個例子:

@apple.id # => 4 
url_for(@apple) #=> domain.com/apples/4 

這等同於更復雜的:

url_for(:controller => 'apples', :id => 4, :method => :show) 

在試圖測試我的意見,我用摩卡嘲笑我的對象。

Apple.stubs(:color => 'red') # returns a MochaExpectation, rather than an instance of Apple. 

所以,在我的測試:

assigns[:apple] = @apple = Apple.stubs(:color => 'red') 
url_for(@apple) #=> raises undefined method `mocha_expectation_path' 

我怎樣才能得到這個辦法?看起來存根需要返回Apple < ActiveRecord :: Base或url_for需要了解如何處理mocha_expectation。

回答

1

您錯誤地使用了Mocha

如果你想有一個傳統的摩卡對象的實例: -

@apple = stub('apple', :colour => 'red') 

如果你想存根一個真正的對象上的具體方法: -

@apple = Apple.new 
@apple.stubs(:colour => 'red')