ruby-on-rails - 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
我会以错误的方式解决这个问题吗?
解决方案
推荐阅读
- flutter - 使用flutter redux,StatefulWidget 是可选的吗?
- php - 在响应中首先排序并显示具有大多数值的数组(php)
- javascript - 将多个过滤器应用于数组javascript
- .net - 添加项目引用时出现 Blazor Webassembly 错误
- google-sheets - 使用 ARRAYFORMULA Google 表格的 TEXTJOIN
- c - 带有 adafruit RGBLCDShield Buttons 奇怪行为的 Arduino
- go - 有条件地停止其他 goroutine
- r - bestglm 函数的变量数限制
- python - 是否可以在 Geopandas 中为颜色映射设置边界?
- php - 如何关闭服务器和浏览器上 PHP 页面的缓存?