简体   繁体   English

使用RSpec在控制器中存根特定方法

[英]Stubbing a specific method in a controller with RSpec

I am trying to stub out a mixin method in my controller tests. 我试图在控制器测试中存根mixin方法。 The controller includes the SeatingChartCreator module, which has already been tested. 该控制器包括SeatingChartCreator模块,该模块已经过测试。 So all I want to do is stub it out so that it sets the controller @errors variable to nil in one test, and to something in another, and then stub it out another time just to make sure that it gets called. 因此,我想要做的就是将它存根,以便在一个测试中将控制器@errors变量设置为nil,在另一个测试@errors其设置为@errors ,然后再将其存根,以确保它被调用。 I've looked over the RSpec documentation, and it seems like all they provide information on is stubbing a whole class, not just specific methods in the class I am testing. 我查看了RSpec文档,似乎他们提供的所有信息都在存根整个类,而不仅仅是我正在测试的类中的特定方法。

To stub a method use allow/receive . 要存根方法,请使用allow/receive For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM