ruby on rails-conference_backages#打印机应该放置徽章和房间分配的列表



因此,当前运行代码时出现以下错误。我在另一个函数中使用了一个函数,我认为这是问题的根源,但指令要求它是这样的。

提示:记住方法可以调用其他方法。如果assign_rooms的返回值是房间分配的数组,如何打印每个分配?您需要迭代您的房间分配数组,以便列出每个单独的分配。

Failures:
  1) conference_badges #printer should puts the list of badges and room_assignments
     Failure/Error: expect($stdout).to receive(:puts).with(line.chomp)
     
       (#<IO:<STDOUT>>).puts("Hello, my name is Edsger.")
           expected: 1 time with arguments: ("Hello, my name is Edsger.")
           received: 0 times
     # ./spec/conference_badges_spec.rb:98:in `block (4 levels) in <top (required)>'
Finished in 0.02707 seconds (files took 0.28753 seconds to load)
4 examples, 1 failure

当我运行以下代码时,它给出了:

def badge_maker(name)
  return "Hello, my name is #{name}."
end
def batch_badge_creator(names)
  greetings = [] # initialize greetings as an empty array
  names.each do |name| # for each name in the names array
    greetings <<  badge_maker(name)# add a greeting for that name
  end
  return greetings # return the array of all greetings, at the end
end
def assign_rooms(speakers)
  greet = []
  speakers.each_with_index{ |speakers, index| greet << "Hello, #{speakers}! You'll be assigned to room #{index+1}!"}
  return greet
  end
def printer(inputOne)
  batch_badge_creator(inputOne)
  assign_rooms(inputOne)
end

但我不明白为什么不匹配Rspec:的输出

 # Question 4
    # The method `printer` should output first the results of the batch_badge_creator method and then of the assign_rooms method to the screen - this way you can output
    # the badges and room assignments one at a time.
    # To make this test pass, make sure you are iterating through your badges and room assignments lists.
    it 'should puts the list of badges and room_assignments' do
      badges_and_room_assignments.each_line do |line|
        # $stdout is a Ruby global varibale that represents the current standard output.
        # In this case, the standard output is your terminal screen. This test, then,
        # is checking to see whether or not your terminal screen receives the correct
        # printed output.
        expect($stdout).to receive(:puts).with(line.chomp)
      end
      printer(attendees)
    end
  end
end

这修复了它:

def badge_maker(name)
  return "Hello, my name is #{name}."
end
def batch_badge_creator(names)
  greetings = [] # initialize greetings as an empty array
  names.each do |name| # for each name in the names array
    greetings <<  badge_maker(name)# add a greeting for that name
  end
  return greetings # return the array of all greetings, at the end
end
def assign_rooms(speakers)
  greet = []
  speakers.each_with_index{ |speakers, index| greet << "Hello, #{speakers}! You'll be assigned to room #{index+1}!"}
  return greet
  end
def printer(attendees)
  resultOne = batch_badge_creator(attendees)
  resultOne.each do |x|
    puts x 
  end
  result = assign_rooms(attendees)
  result.each do |x|
    puts x 
  end
end
  

最新更新