The*_*end 5 controller rspec ruby-on-rails private-methods
我在控制器中有一个私有方法
private
def body_builder
review_queue = ReviewQueueApplication.where(id: params[:review_queue_id]).first
...
...
end
Run Code Online (Sandbox Code Playgroud)
我只想测试该body_builder方法,它是一种为 rest 客户端 api 调用构建有效负载的方法。但是,它需要访问参数。
describe ReviewQueueApplicationsController, type: :controller do
describe "when calling the post_review action" do
it "should have the correct payload setup" do
@review_queue_application = ReviewQueueApplication.create!(application_id: 1)
params = ActionController::Parameters.new({ review_queue_id: @review_queue_application.id })
expect(controller.send(:body_builder)).to eq(nil)
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果我运行上面的它会发送body_builder方法,但它会中断,因为参数没有像在调用操作时那样正确设置。
我总是可以为该body_builder方法创建一个条件参数,以便它接受一个参数,或者它将使用这样的参数def body_builder(review_queue_id = params[:review_queue_id])然后在测试中controller.send(:body_builder, params),但我觉得更改代码以使测试通过是错误的,它应该只是测试它照原样。
在将私有方法发送给控制器之前,如何将参数输入控制器?
我认为你应该能够更换
params = ActionController::Parameters.new({ review_queue_id: @review_queue_application.id })
Run Code Online (Sandbox Code Playgroud)
和
controller.params = ActionController::Parameters.new({ review_queue_id: @review_queue_application.id })
Run Code Online (Sandbox Code Playgroud)
你应该表现得很好。params 只是控制器的一个属性(实际属性是@_params,但有访问该 ivar 的方法。尝试放入controller.inspect视图中)。
| 归档时间: |
|
| 查看次数: |
2876 次 |
| 最近记录: |