1
我的問題:測試失敗 - 一個類方法來調用另一個(「預期:1個時間參數,獲得0次」)
我試圖存根返回該實例的類方法類,但我得到了題爲測試下面的錯誤「創建具有CSV數據的實例」:
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
上下文:
的代碼(如下圖所示)的作品 - QuestionData.load_questions
加載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
測試文件:
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
嗨aledustet,非常感謝回答我的問題,我很抱歉,我纔剛剛意識到我最初發布是不工作應該如何一直在問之前的代碼。我現在已經改變了它,所以它現在可以工作了100%(但我仍然不知道如何測試它,併爲我的測試獲得相同的錯誤)。再次巨大的歉意,真的很感激你花時間。 我已經嘗試將您的'describe_class'想法合併到我的新測試中,但至今還沒有運氣(再次感謝提示!)。 –