我有一个ruby文件aircraft.rb
具有像这样的红宝石类-
class AirplaneSeat
attr_accessor :seat_row, :seat_column, :type, :order, :assigned_passenger
def initialize(seat_row, seat_column, type, order, assigned_passenger = 0)
@seat_row = seat_row
@seat_column = seat_column
@type = type
@order = order
@assigned_passenger = assigned_passenger
end
def get_passenger_seating
#some code
end
end # end of class
# outside the class
begin
puts "Enter the seating matrix as 2D array"
seat_matrix = JSON.parse gets.chomp
puts "Enter the number of passengers"
no_of_passengers = gets.chomp
raise "Please enter a valid passenger count" if (no_of_passengers.empty? || no_of_passengers.to_i <=0)
AirplaneSeat.get_passenger_seating(seat_matrix, no_of_passengers)
rescue Exception => e
puts "Error encountered -> #{e.message}"
end
因此ruby类有一些方法和几行代码可以在类外部执行,这需要用户的输入,然后调用类方法。
我该如何为此编写测试用例?我已经完成了rspecs gem和spec文件夹的设置。 我不太了解如何从测试用例开始。 任何指针,不胜感激。 谢谢!
答案 0 :(得分:1)
举一个简单的例子,我们有一个Foo
类foo.rb
的文件:
class Foo
def call
'bar'
end
end
我们可以创建一个规范,foo_spec.rb
:
require 'rspec'
require_relative 'foo'
RSpec.describe Foo do
describe '#call' do
it 'works' do
expect(described_class.new.call).to eq 'Bar'
end
end
end
然后从命令行运行规范:
$ rspec foo_spec.rb