首页 > 解决方案 > rspec中的存根.push方法

问题描述

我有一些使用.push. 我想编写一个单元测试来测试这段代码的失败场景,但我无法弄清楚语法。

我的代码看起来像这样

   if object.groups.push(group)
    flash[:notice] = I18n.t(".success")
  else
    flash[:alert] = I18n.t(".failure")
  end

我试图存根该.push方法,使其返回false,但它仍然返回true。

我的存根尝试如下:

      object.groups.should_receive(:push).with(group).and_return(false)
  allow(object.groups).to receive(:push).with(group).and_call_original

我会以错误的方式解决这个问题吗?

标签: ruby-on-railsrubyrspecstubbing

解决方案


推荐阅读