用rspec测试ruby程序

用rspec测试ruby程序,ruby,testing,rspec,bdd,Ruby,Testing,Rspec,Bdd,现在我正在学习ruby,我想学习如何使用rspec和BDD开发应用程序,但我以前从未做过任何类型的测试。我很难用“写行为->做方向”来思考。我已经找到了一些简单的rspec初学者教程,但是对于我的例子来说,没有太多帮助,因为我没有类就构建了一个小程序 这是我的密码。简单的Cesar密码旋转程序。快速简短…当用户输入以下0-26和文本时,例如ROT24一些文本 它检查输入的格式是否正确且在范围内,然后根据字后的数字旋转文本中的字符。示例ROT5按字母顺序将字符旋转5个点 i=0 #check in

现在我正在学习ruby,我想学习如何使用rspec和BDD开发应用程序,但我以前从未做过任何类型的测试。我很难用“写行为->做方向”来思考。我已经找到了一些简单的rspec初学者教程,但是对于我的例子来说,没有太多帮助,因为我没有类就构建了一个小程序

这是我的密码。简单的Cesar密码旋转程序。快速简短…当用户输入以下0-26和文本时,例如ROT24一些文本 它检查输入的格式是否正确且在范围内,然后根据字后的数字旋转文本中的字符。示例ROT5按字母顺序将字符旋转5个点

i=0
#check input

while true

puts
    if i>0
        puts "Wrong Entry Please Try Again!"

        puts "Input Decipher key in ROT0-ROT26 format and text to decipher using white space between. Example (ROT2 SomeText)"
        input=gets.chop

    else
        puts "Input Decipher key in ROT0-ROT26 format and text to decipher using white space between. Example (ROT2 SomeText)"
        input=gets.chop
        i+=1
    end

 break if input.match(/\AROT([0-9]|1[0-9]|2[0-6]) /) 

end



#splitting input
inputArray=input.split(" ",2)

inputFirstPart= inputArray[0]

inputKey=inputFirstPart[3..4].to_i

inputText= inputArray[1]

#cipher method
def rotate (str,num)

    alphabetLow = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
    alphabetUp = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
    second_step=str.tr(alphabetLow.join,alphabetLow.rotate(num).join)
    second_step.tr(alphabetUp.join,alphabetUp.rotate(num).join)

end
#output result 
print "Your result is: "
print rotate(inputText,inputKey)
puts
如果有人能为这个可怜的灵魂腾出一些时间,为这个代码编写rspec测试,这样我就可以对它进行反向工程了吗?我尝试了几件事,但最重要的是我对用户输入感到困惑,因为在测试期间,它要求我实际进行输入,这对我来说毫无意义


提前感谢。

首先,您需要将实际要测试的代码部分拆分到一个单独的文件中,该文件将代码放入一个类中。然后,您可以在正在运行的文件中要求该代码,接受来自输入的数据,并将该数据传递给它。同时,等级库文件也需要该文件,并针对类中的方法运行等级库。可能看起来像:

rotator.rb:

class Rotator
    def rotate(str, num)
        #do stuff
    end
end
旋转器规格rb

require_relative 'rotator'
describe Rotator do
    it 'rotates' do
        expect(described_class.new.rotate('a', 'b')).to eq 'result'
    end
end
run.rb

require_relative 'rotator'
rotator = Rotator.new
#do input stuff
puts rotator.rotate(inputText, inputKey)

首先,您需要将实际要测试的代码部分拆分到一个单独的文件中,该文件将代码放入一个类中。然后,您可以在正在运行的文件中要求该代码,接受来自输入的数据,并将该数据传递给它。同时,等级库文件也需要该文件,并针对类中的方法运行等级库。可能看起来像:

rotator.rb:

class Rotator
    def rotate(str, num)
        #do stuff
    end
end
旋转器规格rb

require_relative 'rotator'
describe Rotator do
    it 'rotates' do
        expect(described_class.new.rotate('a', 'b')).to eq 'result'
    end
end
run.rb

require_relative 'rotator'
rotator = Rotator.new
#do input stuff
puts rotator.rotate(inputText, inputKey)

首先,让我们将代码重构为自己的类:

凯撒

class Caesar
  def self.rotate (str, num)
    alphabet_low = ('a'..'z').to_a # Shorthand for alphabet creation, thanks Ruby!
    alphabet_high = ('A'..'Z').to_a

    second_step = str.tr(alphabet_low.join, alphabet_low.rotate(num).join)
    second_step.tr(alphabet_high.join, alphabet_high.rotate(num).join)
  end
end
接下来,我们需要安装rspec gem:

$ gem install rspec
让我们添加一个spec目录并在其中创建一个rspec文件:

规范/凯撒_规范rb

require_relative '../caesar'

describe Caesar do
  describe '.rotate' do
    context 'when number 0 is provided' do
      let(:number) { 0 }

      it 'returns the same text that is provided' do
        string = 'Hello World'
        expect(Caesar.rotate(string, number)).to eq('Hello World')
      end
    end

    context 'when number 1 is provided' do
      let(:number) { 1 }

      it 'encrypts the given string by rotating it a single character' do
        string = 'Hello World'
        expect(Caesar.rotate(string, number)).to eq('Ifmmp Xpsme')
      end
    end
  end
end
现在,从项目文件夹中的命令行运行rspec:

$ rspec
这将导致:

..

Finished in 0.00222 seconds (files took 0.09387 seconds to load)
2 examples, 0 failures

首先,让我们将代码重构为自己的类:

凯撒

class Caesar
  def self.rotate (str, num)
    alphabet_low = ('a'..'z').to_a # Shorthand for alphabet creation, thanks Ruby!
    alphabet_high = ('A'..'Z').to_a

    second_step = str.tr(alphabet_low.join, alphabet_low.rotate(num).join)
    second_step.tr(alphabet_high.join, alphabet_high.rotate(num).join)
  end
end
接下来,我们需要安装rspec gem:

$ gem install rspec
让我们添加一个spec目录并在其中创建一个rspec文件:

规范/凯撒_规范rb

require_relative '../caesar'

describe Caesar do
  describe '.rotate' do
    context 'when number 0 is provided' do
      let(:number) { 0 }

      it 'returns the same text that is provided' do
        string = 'Hello World'
        expect(Caesar.rotate(string, number)).to eq('Hello World')
      end
    end

    context 'when number 1 is provided' do
      let(:number) { 1 }

      it 'encrypts the given string by rotating it a single character' do
        string = 'Hello World'
        expect(Caesar.rotate(string, number)).to eq('Ifmmp Xpsme')
      end
    end
  end
end
现在,从项目文件夹中的命令行运行rspec:

$ rspec
这将导致:

..

Finished in 0.00222 seconds (files took 0.09387 seconds to load)
2 examples, 0 failures