gpt4 book ai didi

ruby-on-rails - Rspec, rails : how to test private methods of controllers?

转载 作者:行者123 更新时间:2023-12-03 04:32:51 44 4
gpt4 key购买 nike

我有 Controller :

class AccountController < ApplicationController
def index
end

private
def current_account
@current_account ||= current_user.account
end
end

如何使用 rspec 测试私有(private)方法 current_account

附注我使用 Rspec2 和 Ruby on Rails 3

最佳答案

使用#instance_eval

@controller = AccountController.new
@controller.instance_eval{ current_account } # invoke the private method
@controller.instance_eval{ @current_account }.should eql ... # check the value of the instance variable

关于ruby-on-rails - Rspec, rails : how to test private methods of controllers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4271696/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com