使用 RSpec 在控制器中添加特定方法

Stubbing a specific method in a controller with RSpec

我正试图在我的控制器测试中剔除混合方法。控制器包括已经过测试的 SeatingChartCreator 模块。所以我想做的就是将它存根,以便它在一个测试中将控制器 @errors 变量设置为 nil,在另一个测试中将其设置为某物,然后再将其存根以确保它被调用。我查看了 RSpec 文档,似乎他们提供的所有信息都是对整个 class 进行存根,而不仅仅是我正在测试的 class 中的特定方法。

要对方法进行存根,请使用 allow/receive。例如:

before do
  allow(controller).to receive(:say_hello).and_return('Hello!')
end