2

我的问题:

我正在尝试对返回该类实例的类方法进行存根,但是对于题为“使用 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
4

1 回答 1

0

问题是您正在取消通话:

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

如果您仍然希望调用执行,您需要添加:

and_call_original

因此,原始方法将被执行,您的代码将在原始块上调用新方法。

但问题是你不需要存根你只需要更改存根的类,因为你在双精度上调用该方法,它会尝试在一个类中执行它,所以你可能需要改变你的第二个测试:

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
于 2017-02-24T13:59:05.383 回答