首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Rails:如何测试控制器的私有方法?

Rails:如何测试控制器的私有方法?
EN

Stack Overflow用户
提问于 2010-11-25 05:26:51
回答 9查看 59.9K关注 0票数 126

我有控制器:

代码语言:javascript
复制
class AccountController < ApplicationController
  def index
  end

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

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

附言:我使用Rspec2和Ruby on Rails 3

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2010-11-25 05:48:40

使用#instance_eval

代码语言:javascript
复制
@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
票数 196
EN

Stack Overflow用户

发布于 2012-05-28 19:57:06

我使用send方法。例如:

代码语言:javascript
复制
event.send(:private_method).should == 2

因为"send“可以调用私有方法。

票数 39
EN

Stack Overflow用户

发布于 2010-11-25 05:29:57

在哪里使用了current_account方法?它的作用是什么?

通常,您不测试私有方法,而是测试调用私有方法的方法。

票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4271696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档