0

我有以下规格:

describe EmailImport do
  describe "#format_x_priority" do
    specify { EmailImport.format_x_priority('5 (Lowest)').should eq(:low) }

    it "should raise an error when wrong argument is passed" do
      expect { EmailImport.format_x_priority('5 (Lowest)').to raise_error }
    end
  end
end

这应该不会过去吧?两次都是相同的代码,一次应该导致:low,一次应该导致错误。但有趣的是,两者都通过了!我究竟做错了什么?

只是为了完整起见,这是我的EmailImport.format_x_priority方法:

def self.format_x_priority(priority)
  # 1 Highest; 2 High; 3 oder "" normal; 4 low; 5 lowest
  case priority
  when "1 (Highest)"
    Priority::HIGH
  when "2 (High)"
    Priority::HIGH
  when "3 (Middle)"
    Priority::MEDIUM
  when "4 (Low)"
    Priority::LOW
  when "5 (Lowest)"
    Priority::LOW
  when ''
    Priority::MEDIUM
  else
    raise "Invalid value '#{priority}' for argument 'priority'!"
  end
end
4

2 回答 2

2

语法错误

您有语法错误。您将整行包含在块中,包括匹配器。移动花括号以仅包含 lambda,您应该回到正轨。例如:

it "should raise an error when wrong argument is passed" do
  expect { EmailImport.format_x_priority('5 (Lowest)') }.to raise_error
end

为什么这样有效

指定关键字需要一个块,所以specify { ... }是正确的。另一方面,expect接受一个 lambda,并将其传递给匹配器。那是expect { ... }.to raise_error正确的。

于 2012-07-17T14:57:55.857 回答
0

解决方案在于细节!正确的代码:

expect { EmailImport.format_x_priority('5 (Lowest)') }.to raise_error

错误代码:

expect { EmailImport.format_x_priority('5 (Lowest)').to raise_error }
于 2012-07-17T14:38:27.960 回答