2017-02-24 4 views
1

Mein Problem:Test nicht bestehen - eine Klassenmethode eines anderen zu nennen („erwartet: 1 mal mit Argumenten, erhielt 0 mal“)

Ich versuche, eine Klassenmethode Stummeln, das eine Instanz, dass zurückzugibt Klasse, aber ich erhalte die folgende Störung für den Test des Titels "erstellt eine Instanz mit CSV-Daten":

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 

der Kontext:

der Code (siehe unten) arbeitet - QuestionData.load_questions lädt Daten aus einer CSV-Datei und ruft QuestionData.new mit den Daten als Argument auf. Mein Test für die .load_questions Methode gibt jedoch den obigen Fehler. Wenn es aufgerufen wird, erhält das Double der QuestionData Klasse nicht den Stub von .new mit dem data Doppel.

Ich habe versucht zu erforschen, wie man Stubs testet, die einen anderen Stub oder eine Instanz zurückgeben, aber scheint keine relevante Antwort zu finden.

Ich würde wirklich jede Hilfe oder Beratung zu schätzen wissen, vielen Dank im Voraus!

Der Code:

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 

Die Testdatei:

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 

Antwort

0

Die Sache ist, dass Sie den Anruf Anstoßen auf:

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

Wenn Sie noch möchte, dass der Aufruf ausgeführt wird Sie benötigen ein hinzuzufügen:

and_call_original 

Deshalb ist die ursprüngliche Methode der Code ausgeführt wird und die neue Methode aufrufen, auf dem ursprünglichen Block.

Aber die Sache ist, dass Sie nicht die Klasse stub müssen Sie nur die Stubs ändern, weil Sie die Methode auf eine Doppel aufrufen, und es wird versuchen, es in einer Klasse auszuführen, so dass Sie benötigen zu ändern, um Ihren zweiten Test:

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

Hallo aledustet, Vielen dank, meine Frage zu beantworten, ich bin so traurig, ich den Code nur gerade realisiert, ursprünglich geschrieben ich war nicht funktioniert, wie es vorher zu fragen sein sollte . Ich habe es jetzt leicht verändert und es funktioniert jetzt 100% (aber ich weiß immer noch nicht wie ich es testen soll und bekomme den gleichen Fehler für meinen Test). Nochmals große Entschuldigung, ich schätze wirklich, dass Sie sich die Zeit genommen haben. Ich habe versucht, Ihre 'beschrieb_klass'-Idee in meinen neuen Test zu integrieren, aber bisher kein Glück (Danke nochmal für den Tipp!). –

Verwandte Themen