2017-02-24 3 views
1

私の問題:テスト - 別のを呼び出すための1つのクラスメソッド(「予想:引数付きの1時間は、0回受信」)

私はそれのインスタンスを返すクラスメソッドをスタブしようとしています

Failures: 

    1) QuestionData.load_questions creates an instance with CSV data 
    Failure/Error: expect(question_data_class).to receive(:new).with(data).and_return(question_data_instance) 

     (QuestionData (class)).new([{:time_limit=>10, :text=>"Who was the legendary Benedictine monk who invented champagne?", :correct_...the world?", :correct_answer=>"Lake Superior", :option_2=>"Lake Victoria", :option_3=>"Lake Huron"}]) 
      expected: 1 time with arguments: ([{:time_limit=>10, :text=>"Who was the legendary Benedictine monk who invented champagne?", :correct_...the world?", :correct_answer=>"Lake Superior", :option_2=>"Lake Victoria", :option_3=>"Lake Huron"}]) 
      received: 0 times 

コンテキスト:: -(下記に示す)

コードが動作するクラスが、私は、「CSVデータを持つインスタンスが作成されます」と題されたテストのために、次のエラーを取得していますは、CSVファイルからデータをロードし、そのデータを引数としてQuestionData.newを呼び出します。しかし、.load_questionsメソッドのテストでは、上記のエラーが発生しています。それが呼び出されると、QuestionDataクラスの倍精度は.newのスタブをdata倍で受け取っていません。

別のスタブまたはインスタンスを返すスタブをテストする方法を調査しましたが、関連する回答が見つからないようです。

私は本当に助けて頂きありがとうございます。事前に感謝します!

コード:

require "csv" 

class QuestionData 

    attr_reader :questions 

    def initialize(questions) 
    @questions = questions 
    end 

    def self.load_questions(file = './app/lib/question_list.csv', questions = []) 
    self.parse_csv(file, questions) 
    self.new(questions) 
    end 

    def self.parse_csv(file, questions) 
    CSV.foreach(file) do |row| 
     time_limit, text, correct_answer, option_2, option_3 = row[0], 
     row[1], row[2], row[3], row[4] 
     questions << { time_limit: time_limit, text: text, 
     correct_answer: correct_answer, option_2: option_2, option_3: option_3 
     } 
    end 
    end 

end 

テストファイル:まだあなた場合

allow(question_data_class).to receive(:load_questions).with(file) 

require './app/models/question_data' 

describe QuestionData do 

    subject(:question_data_instance) { described_class.new(data) } 
    let(:question_data_class) { described_class } 
    let(:CSV) { double(:CSV, foreach: nil) } 
    let(:questions) { [] } 
    let(:file) { double(:file) } 
    let(:data) do 
    [{ 
     time_limit: 10, 
     text: "Who was the legendary Benedictine monk who invented champagne?", 
     correct_answer: "Dom Perignon", 
     option_2: "Ansgar", 
     option_3: "Willibrord" 
     }, 
     { 
     time_limit: 12, 
     text: "Name the largest freshwater lake in the world?", 
     correct_answer: "Lake Superior", 
     option_2: "Lake Victoria", 
     option_3: "Lake Huron" 
     }] 
    end 

    describe '#questions' do 
    it "has an array of question data from CSV" do 
     expect(question_data_instance.questions).to eq(data) 
    end 
    end 

    describe '.parse_csv' do 
    it "parses CSV data into hash data" do 
     expect(CSV).to receive(:foreach).with(file) 
     question_data_class.parse_csv(file, questions) 
    end 
    end 

    describe '.load_questions' do 
    it "calls '.parse_csv' method" do 
     expect(question_data_class).to receive(:parse_csv).with(file, questions) 
     question_data_class.load_questions(file, questions) 
    end 

    it "creates an instance with CSV data" do 
     allow(question_data_class).to receive(:load_questions).with(file, questions).and_return(question_data_instance) 
     allow(question_data_class).to receive(:new).with(data).and_return(question_data_instance) 
     expect(question_data_class).to receive(:new).with(data).and_return(question_data_instance) 
     question_data_class.load_questions(file, questions) 
    end 
    end 

    describe '.new' do 
    it "creates a new instance with CSV data" do 
     expect(question_data_class).to receive(:new).with(data).and_return(question_data_instance) 
     question_data_class.new(data) 
    end 
    end 

end 

答えて

0

事はあなたが上の呼び出しをスタブされていることです呼び出しを実行したい

and_call_original 

したがって、元のメソッドが実行され、コードで元のブロックの新しいメソッドが呼び出されます。

しかし、スタブを変更する必要があるクラスをスタブする必要はありません。これは、ダブルでメソッドを呼び出すためで、クラス内でメソッドを実行しようとするため、必要になる可能性があります2番目のテストを変更するには:

describe '.load_questions' do 
    it "creates an instance containing CSV data" do 
    expect(described_class).to receive(:new).with(data).and_return(question_data_instance) 
    described_class.load_questions(file) 
    end 
end 
+0

こんにちはaledustet、私の質問に答えるために多くのおかげで、私は、私はちょうど私はもともとそれが尋ねる前にされている必要がありますどのように働いていなかった掲示コードを実現ごめんなさい。私は今それをわずかに変更しました.100%は今でも動作します(しかし、私はまだそれをテストし、私のテストで同じエラーを得る方法はわかりません)。再び大きな謝罪、本当にあなたが時間をかけていただきありがとうございます。 私はあなたの 'describe_class'アイデアを私の新しいテストに取り入れようとしましたが、まだまだ幸運はありません。 –

関連する問題