我有一个观点,它调用了一个助手。在我的 rspec 测试中,我想检查是否使用适当的属性调用了此帮助程序。
<div class="chart">
<%= chart_tag(:pie, {css: 0.1, javascript: 0.3, ruby: 0.6}) %>
</div>
我不想测试chart_tag
是否呈现正确的 HTML;它是一个设置各种data-
属性的复杂助手;它设置了 highcharts 参数。此方法本身经过适当测试。
我想测试类似的东西:
expect(TemplateClass::SomeHelper).to receive(:chart_tag).with(:pie)
我不知道在哪里设定期望:我不知道细胞如何渲染事物。它似乎利用了 rails 的渲染堆栈,但这对我来说也是一个陌生的领域。
我通过cells调用它,所以泛型rspec view
在这里不适用。
在单元格中,助手包含在helper
中,据我所知,这是一种rails 方法。但是按照该代码,仍然没有告诉我视图中包含什么,通过什么代理、类或模块。
为了说明堆栈/代码的一部分:
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
知道要测试什么消息期望吗?