簡體   English   中英

RSpec如何在注入方法中進行測試

[英]Rspec how to test in inject method

我有一個看起來像這樣的方法:

  def self.average_top_level_comments_leaders
    top_level_comment_count = CrucibleComment.group(:user_id).where(parent_comment_id: nil).order('count_all DESC').count
    code_review_assigned_count = Reviewer.group(:user_id).order('count_all DESC').count

    division_result = top_level_comment_count.inject({}) do |result, item|
      id = item.first #id =12
      count = item.last #value = 57
      if (count && code_review_assigned_count[id]) 
        result[id] = (count/ code_review_assigned_count[id]).round(2) 
        #result[12] = 57/12 = 3.3, => {12, 3.3}
      end
      result 
    end
  end

此方法返回一個散列,其ID為鍵,而除法的結果為值。

我已經成功測試了top_level_comment_count和code_review_assigned計數,但是在弄清楚如何測試do塊中的其他4件事時遇到了麻煩:

.first, .last, .round(2), result

我正在嘗試測試.first,這是我到目前為止的結果:

describe '#average_top_level_comments_leaders' do
    subject { User.average_top_level_comments_leaders}

    let(:avg_top_level_comments) { double }
    let(:code_review_count) { double }
    let(:item) { double( {id: 12}) }

    context 'when getting the comment count succeeds ' do
      before do
        allow(CrucibleComment).to receive(:group).with(:user_id).and_return(avg_top_level_comments)
        allow(avg_top_level_comments).to receive(:where).with(parent_comment_id: nil).and_return(avg_top_level_comments)
        allow(avg_top_level_comments).to receive(:order).with('count_all DESC').and_return(avg_top_level_comments)
        allow(avg_top_level_comments).to receive(:count).and_return(avg_top_level_comments)

        allow(avg_top_level_comments).to receive(:inject).and_return(avg_top_level_comments)
        allow(item).to receive(:first).and_return(item)

        allow(Reviewer).to receive(:group).with(:user_id).and_return(code_review_count)
        allow(code_review_count).to receive(:order).with('count_all DESC').and_return(code_review_count)
        allow(code_review_count).to receive(:count).and_return(code_review_count)
        allow(code_review_count).to receive(:round).with(2).and_return(code_review_count)
      end

      it 'and the correct parameters are called' do
        expect(CrucibleComment).to receive(:group).with(:user_id)
        subject
      end

      it 'and comment count is calling descending correctly' do
        expect(avg_top_level_comments).to receive(:order).with('count_all DESC')
        subject
      end

      it 'item gets the first result' do
        expect(item).to receive(:first)
        subject
      end
    end
  end

我無法傳遞最后的it語句。 我試圖期望(item).receive(:first),但是它在錯誤中這樣說:

失敗/錯誤:Expect(item).to receive(:first)(Double).first(*(any args))預期:1次接收到任何參數:0次使用任何參數

知道為什么沒有通過嗎? 其他兩個正在過去

item的雙從未在測試中使用,所以當它到達:

expect(item).to receive(:first)

它失敗。

如果您希望將double item用於此處的inject塊中:

division_result = top_level_comment_count.inject({}) do |result, item|

僅僅因為它具有相同的名稱,它就不能那樣工作。 您需要在avg_top_level_comments double上定義一個方法,該方法在調用inject時返回double item

但是,您不應該這樣做。 扔掉所有這些,並使用真實的模型實例進行測試。 它將易於閱讀和維護。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM