加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rspec:如何存根私有方法?

发布时间:2020-12-17 03:20:17 所属栏目:百科 来源:网络整理
导读:这是错误: private method `desc' called for #Array:0x0000010532e280 规格: describe SubjectsController do before(:each) do @subject = mock_model(Subject) end describe "#0002 - GET #index" do before(:each) do subjects = [@subject,mock_model
这是错误:

private method `desc' called for #<Array:0x0000010532e280>

规格:

describe SubjectsController do  
  before(:each) do
    @subject = mock_model(Subject)
  end

    describe "#0002 - GET #index" do
    before(:each) do        
      subjects = [@subject,mock_model(Subject),mock_model(Subject)]
      Subject.stub!(:all).and_return(subjects)
      Subject.all.stub!(:desc).and_return(subjects)
      get :index
    end

    it { response.should be_success }
    it { response.should render_template("index") }
  end
end

和控制器:

def index
  @subjects = Subject.all(conditions: {company_id: current_user.company.id}).desc(:created_at)
end

我不知道如何解决这个问题,有人可以帮个忙吗?
你能否就如何测试这种方法给我建议?
谢谢.

解决方法

这是一个更嘲弄的&链接比我更喜欢,但你可以这样做:

subjects = [...]
desc_mock = double("desc order mock")
desc_mock.should_receive(:desc).with(:created_at).and_return(subjects)

conditions = {...}
Subject.should_receive(:all).with(conditions).and_return(desc_mock)

您还可以通过将查询移动到带有一些参数的命名范围来简化这一过程.然后您的测试可以验证主题是否使用正确的参数接收您的范围,例如:

Subject.should_receive(:user_company).with(current_user.id).and_return(subjects)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读