【问题标题】:How to write rspec for logic and write mock STDIN如何为逻辑编写 rspec 并编写模拟 STDIN
【发布时间】:2019-11-07 23:20:00
【问题描述】:

我是一个非常新的编码员,正在尝试为一个测试条件语句/逻辑的类编写 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

【问题讨论】:

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


    【解决方案1】:

    您可以使用method stubs

    在这种情况下,您想要存根 STDIN.gets.chomp,所以您可以这样做:

    describe '#find_part_id' do
      before do
        allow(STDIN.gets).to receive(:chomp).and_return(stdin_input)
      end
    
      let(:stdin_input) { 'user input from stdin' }
      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
          it 'some test' do
            # your test here
          end
        end
    
        # more contexts...
    
        context 'a context that needs a different stdin_input' do 
          let(:stdin_input) { 'some different user input from stdin' }
    
          it 'another test' do
            # your test here
          end
        end
      end
    end
    

    其中 stdin_input 是您希望用户为测试输入的字符串。

    【讨论】:

    • 您可以通过在每个上下文中放置 let(:stdin_input) 来为每个上下文设置不同的 stdin_input。
    • 你能写一个上下文吗,我不知道怎么写?
    • 我编辑了答案以显示更多测试。我真的不知道你还在寻找什么。它不工作吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多