我寫了一個Ruby庫(Rails中偶然使用)一些代碼,提出拋出一個RuntimeError有點像如下:NoMethodError:未定義的方法`RuntimeError」
class MyClass
def initialize(opts = {})
# do stuff
thing = opts[:thing]
raise RuntimeError "must have a thing!" unless thing.present? && thing.is_a?(Thing)
# more stuff
end
end
,當我跑我的全新rspec的規格在它,它看起來有點像:
it "should raise an error if we don't pass a thing" do
lambda {
my_class = MyClass.new(:thing => nil)
}.should raise_exception(RuntimeError)
end
我一直得到一些奇怪:
expected RuntimeError, got
#<NoMethodError: undefined method `RuntimeError' for #<MyClass:0xb5acbf9c>>
啊 - 你回答之前,我有我的答案了:)這實際上是一個「解決前」的問題......我想別人去同樣的問題,並可能想要的答案太... – 2011-04-22 15:50:32