2015-02-06 15 views
0

我有一個看法,那就是幫助者。在我的rspec測試中,我想檢查這個助手是否被調用了適當的屬性。當從視圖中調用時,希望使用哪個類的輔助方法?

<div class="chart"> 
    <%= chart_tag(:pie, {css: 0.1, javascript: 0.3, ruby: 0.6}) %> 
</div> 

我不想測試chart_tag是否呈現正確的HTML;它是一個複雜的幫手,它可以設置各種各樣的data-屬性;它設置了highcharts params。該方法本身已經過正確測試。

我想測試是這樣的:

expect(TemplateClass::SomeHelper).to receive(:chart_tag).with(:pie) 

我不知道在哪裏設置的期望:我不知道細胞是如何呈現的東西。它似乎利用了rails的渲染堆棧,但對我而言,這也是一個陌生的領域。

我打電話給這個通過cells,所以通用rspec view這裏不適用。

在單元中,助手包含在helper中,據我所知,它是一個rails method。但是遵循這些代碼,仍然不會告訴我視圖中包含什麼,通過代理,類或模塊。

爲了說明堆疊/代碼的一部分:

class AnalysisCell < Cell::Rails 
    helper ChartHelper 

    def chart(params) 
    @type = params[:type] || :pie 
    @dataset = params[:dataset] || {} 
    render # Cells will now, magically, parse and process app/cells/analysis/chart.html.erb 
    end 
end 

app/cells/analysis/chart.html.erb

<%= debugger %> 
<%= chart_tag(:pie, {css: 0.1, javascript: 0.3, ruby: 0.6}) %> 

app/helpers/chart_helper.rb

module ChartHelper 
    def chart_tag(type, dataset) 
    end 
end 

當落入調試器,我可以從內的檢查狀態 erb

self.class #=> #<Class:0x007f9eef0215a8> 
self._helpers #=> NoMethodError Exception: undefined method `_helpers 
self.methods.sort #=> 
# [... 
# :cell_for, 
# :chart_tag, 
# :check_box_tag, 
# ...] 
# self.method(:chart_tag).owner #=> ChartHelper 

但測試失敗,received: 0 times with any argument

it 'renders a chart' do 
    expect(ChartHelper).to receive(:chart_tag) #.with(:pie, {}) 
    render_cell(:analysis, :chart, type: :pie, dataset: {}) 
end 

任何想法是要測試的消息有何期待?

+0

盲目的猜測:作爲助手是一個模塊,因此混合到渲染器類,則其誰收到的消息,而不是模塊渲染器類。所以你的期望應該是針對渲染器 – Benj 2015-02-06 10:00:04

+0

和另一個猜測:'chart_tag'是實例方法(不是類) – gotva 2015-02-06 10:13:16

+0

和另一種猜測:'expect(ChartHelper).to接收(:chart_tag)'應該是'expect_any_instance_of(AnalysisCell).to接收(:chart_tag)' – scorix 2015-02-06 10:20:37

回答

0

在我看來規格,我通常會寫

expect(view).to receive(:chart_tag).with(:pie) 
+0

除了RSpec,你還使用了一顆寶石嗎?查看您定義的內容,還是RSpec可以識別? – onebree 2015-10-30 15:55:27

相關問題