测试在Rails助手中使用嵌套部分渲染部分

时间:2015-08-11 17:50:49

标签: ruby-on-rails rspec

我有一个看起来像这样的帮手:

module ActivityFeedHelper
  def render_activity_feed(activities)
    render partial: 'activities/activity_feed', locals: { activities: activities }
  end
end

我正在编写一个看起来像帮助器的规范:

describe ActivityFeedHelper do
  describe '.render_activity_feed' do
    let(:activities) { FactoryGirl.create_list(:activity, 3) }

    it 'should render activity feed' do
      expect(render_activity_feed(activities)).to render_template(partial: 'activities/activity_feed')
    end
  end
end

但是这给了我错误:

Failure/Error: expect(render_activity_feed(activities)).to render_template(partial: 'activities/activity_feed')                                                                                                                          
       expecting partial <activities/activity_feed> but action rendered <["activities/_activity", "_activity", "activities/_activities", "_activities", "activities/_activity_feed", "_activity_feed"]>.                                      
       Expected {"activities/_activity"=>3, "_activity"=>3, "activities/_activities"=>1, "_activities"=>1, "activities/_activity_feed"=>1, "_activity_feed"=>1} to include "activities/activity_feed".

可能这种情况正在发生,因为部分呈现多个子部分。

那说,我应该怎么测试呢?

1 个答案:

答案 0 :(得分:2)

我认为我曾经感受过这个陷阱。您的错误意味着您必须以这种方式呈现模板:

expect(render_activity_feed(activities)).to render_template(partial: 'activities/_activity_feed')

请注意_activity_feed中的前导下划线。