我有以下规格:
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
答案 0 :(得分: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
更正。
答案 1 :(得分:0)
解决方案在于细节!正确的代码:
expect { EmailImport.format_x_priority('5 (Lowest)') }.to raise_error
错误的代码:
expect { EmailImport.format_x_priority('5 (Lowest)').to raise_error }