当RSpec测试时,Ruby的&&逻辑表现不一致

Are*_*epo 0 ruby rspec boolean-expression

我有一个方法,包括(在后续行中)以下代码:

candidate_lines.each do |line| 
  return line if priority_1?(line) && line.marked_by?(self)
end
Run Code Online (Sandbox Code Playgroud)

据我所知,其中的方法是无关紧要的.是什么让我感到困惑,我有一个测试应该只是通过我给出的规范跳过该方法的一部分:

it "without any priority 1 lines, returns all priority 2 lines marked by itself" do
  line1 = double :line, marked_by?: true
  line2 = double :line, marked_by?: true
  allow(joshua).to receive(:priority_1?).and_return(:false)
  expect(joshua).to receive(:candidate_lines).and_return([line1, line2])
  expect(joshua.tiebreak_lines).to eq [line1, line2]
end
Run Code Online (Sandbox Code Playgroud)

然而,当我运行测试时,返回语句将被触发.我已经在'return line ...'行之前使用puts语句手动测试了它,结果是,'priority_1?(line)'返回false,由于某种原因'priority_1?(line)&& line. marked_by?(self)'正在评估为true.

这里发生了什么事?

Fre*_*ung 6

您将符号:false与布尔值混淆false

因为:false既不是假的,也不为零,这是truthy,那就是尽可能的东西&&if者来说,这是事实.