尝试使用一个类(自行车)中的方法在我的第二类(DockingStation)中实现

发布于 2025-02-03 09:35:06 字数 1787 浏览 2 评论 0原文

   class DockingStation
  
  attr_reader :bikes, :capacity
  attr_accessor :capacity
  
  DEFAULT_CAPACITY = 20
  
 def initialize(capacity = DEFAULT_CAPACITY)
   @bikes = []  
   @capacity = capacity
 end
 

 
  def release_bike
    fail 'No bikes available' if empty?
    @bikes
  end
  
  def dock(bike)
    fail 'bike full' if full?
    @bikes << bike
  end
  
  def full?
    @bikes.count >= DEFAULT_CAPACITY
  end
  
  def empty?
    @bikes.empty?
  end
  

  private :full? , :empty? 
  
end
 class Bike
    def working?
        true
      end
      def report_broken
         @broken = true
      end
      def broken?
        @broken
      end
    end
    require './lib/docking_station.rb'
    require './lib/bike.rb'
    describe DockingStation  do
        describe 'initialization' do
            subject{DockingStation.new}
            let(:bike) {Bike.new}
            it 'default capacity' do 
                described_class::DEFAULT_CAPACITY.times do
                    subject.dock(bike) 
                end
                expect{ subject.dock(bike) }.to raise_error 'bike full'
             end
        end    
        it 'has a default capacity' do
        expect(subject.capacity).to eq DockingStation::DEFAULT_CAPACITY
      end
         describe '#release_bike' do
            it 'releases a bike' do
                 bike = Bike.new

我希望我的RSPEC测试通过(下方),但我一直遇到任何没有提出的错误。我的目标是让我的车站不释放停靠时报告的破损自行车,但是方法破了?是否在我的自行车文件中,所以我无法用我的方法写“失败的“自行车损坏”,如果损坏了?由于对接站文件无法识别该方法。在正确方向上的任何帮助都非常感谢

it 'raises an error if bike is broken' do
           bike = Bike.new
           bike.report_broken
           subject.dock bike
           expect {subject.release_bike}.to raise_error 'No bikes available'
        end
    
   class DockingStation
  
  attr_reader :bikes, :capacity
  attr_accessor :capacity
  
  DEFAULT_CAPACITY = 20
  
 def initialize(capacity = DEFAULT_CAPACITY)
   @bikes = []  
   @capacity = capacity
 end
 

 
  def release_bike
    fail 'No bikes available' if empty?
    @bikes
  end
  
  def dock(bike)
    fail 'bike full' if full?
    @bikes << bike
  end
  
  def full?
    @bikes.count >= DEFAULT_CAPACITY
  end
  
  def empty?
    @bikes.empty?
  end
  

  private :full? , :empty? 
  
end
 class Bike
    def working?
        true
      end
      def report_broken
         @broken = true
      end
      def broken?
        @broken
      end
    end
    require './lib/docking_station.rb'
    require './lib/bike.rb'
    describe DockingStation  do
        describe 'initialization' do
            subject{DockingStation.new}
            let(:bike) {Bike.new}
            it 'default capacity' do 
                described_class::DEFAULT_CAPACITY.times do
                    subject.dock(bike) 
                end
                expect{ subject.dock(bike) }.to raise_error 'bike full'
             end
        end    
        it 'has a default capacity' do
        expect(subject.capacity).to eq DockingStation::DEFAULT_CAPACITY
      end
         describe '#release_bike' do
            it 'releases a bike' do
                 bike = Bike.new

I want my rspec test to pass( down below) but I keep getting the error that nothing was raised. My goal is for my station to not release a broken bike that was reported when docked but the method broken? is in my bike file and so I can't in my method write 'fail 'bike broken' if broken? as the docking station file doesn't recognise the method. Any help in the right direction is greatly appreciated

it 'raises an error if bike is broken' do
           bike = Bike.new
           bike.report_broken
           subject.dock bike
           expect {subject.release_bike}.to raise_error 'No bikes available'
        end
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2025-02-10 09:35:06

如果停靠站没有自行车或仅损坏的自行车,您的测试看起来会引起错误。

it 'raises an error if bike is broken' do
  bike = Bike.new
  bike.report_broken
  subject.dock bike
  expect {subject.release_bike}.to raise_error 'No bikes available'
end

但是您的Release_bike当前没有考虑到损坏的自行车:

def release_bike
  fail 'No bikes available' if empty?
  @bikes
end

我想如果将该方法更改为以下方法,则您的测试应通过:

def release_bike
  available_bikes = @bikes.select { |bike| !bike.broken? }
  fail 'No bikes available' if available_bikes.empty?
  available_bikes
end

哪个首先从@bikes 不损坏的数组,然后仅检查是否有不间断的自行车。

Your test looks like it should raise an error if there are no bikes in a docking station or only broken bikes.

it 'raises an error if bike is broken' do
  bike = Bike.new
  bike.report_broken
  subject.dock bike
  expect {subject.release_bike}.to raise_error 'No bikes available'
end

But your release_bike method currently doesn't take broken bikes into account:

def release_bike
  fail 'No bikes available' if empty?
  @bikes
end

I guess your test should pass if you change that method to the following:

def release_bike
  available_bikes = @bikes.select { |bike| !bike.broken? }
  fail 'No bikes available' if available_bikes.empty?
  available_bikes
end

Which first selects all bikes from the @bikes array that are not broken and then checks only for if there are non-broken bikes available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文