如何为逻辑编写Rspec并编写模拟STDIN

时间:2019-06-26 05:08:44

标签: ruby ruby-on-rails-4 rspec

我是一个非常新的编码人员,正在尝试为测试条件语句/逻辑的类编写rspec。我开始为此进行sudo编码,但被告知要制作模拟STDIN,但我不知道该怎么做。有人可以为该类编写rspec还是让我知道如何创建模拟STDIN。我需要为条件语句/逻辑编写rspec的帮助,如果可以请只为其中一个上下文编写测试,那么我可以根据此做休息。

require 'rails_helper'

module BAB::ACA

 RSpec.describe partfinder do

    describe '#find_part_id' do
      let(:face) { create(:face) }

      subject { described_class.find_part_id(face) }

      context 'When bab con already exists' do
        context 'when there are more than one part ids' do
          #create part ids 

        context 'when user input matches an existing id' do 
          #mock STDIN that matches an existing, subject should equal that id 
        end 

        context 'when user input does not match an existing id' do
          # mock STDIN that does match existing id, should return failure message
        end 
       end 

        context 'when there is only one bab part id' do
          # subject should equal the one that already exists
        end
       end 

        context 'when av con does not yet exist' do
          # mock STDIN and make sure subject equals what you mocked
        end
      end 
end

module BAB::ACA

    class partfinder

      def self.find_part_id(face)
        av_con = BAB::Child:Fail.find_by(
                  face: face
                  reg: BAB:Child.find_reg
                )
        if av_con
          look_id(face, av_con)
        end
        else
          puts "What is #{face.name} BAB part id? must be 6"
          STDIN.gets.chomp
        end
      end

      def self.look_id(face, av_con)
        if av_con.part_ids.length > 1
          ask_for_id(face, av_con)
        else
        av.con.part_ids.first
        end
      end

      def self.ask_for_id(face, av_con)
        puts "What is #{face.name} BAB part id? "
        bab_part_id = STDIN.gets.chomp

        unless av.con.part_ids.include?(bab_part_id)
          fail 'Entered id doesn't match'
        end
        bab_part_id
      end
    end
end

1 个答案:

答案 0 :(得分:1)

您可以使用method stubs

在这种情况下,您想存根public AppShell() { InitializeComponent(); Shell.SetTabBarIsVisible(this, false); } ,因此您将执行以下操作:

STDIN.gets.chomp

stdin_input是您要用户为测试输入的字符串。