即使该方法引发错误,Rspec raise_error测试也会失败

时间:2015-10-10 22:01:01

标签: ruby-on-rails ruby rspec

我的测试看起来像这样:

it 'does return an error when passing a non-subscription or trial membership' do expect(helper.description_for_subscription(recurring_plan)).to raise_error(RuntimeError) end

我的方法返回:

fail 'Unknown subscription model type!'

然而Rspec带着这条失败的消息回来了:

Failure/Error: expect(helper.description_for_subscription(recurring_plan)).to raise_error(RuntimeError) RuntimeError: Unknown subscription model type!

发生了什么事?

1 个答案:

答案 0 :(得分:6)

您应该使用{}而不是()将期望包装在一个块中:

expect{
  helper.description_for_subscription(recurring_plan)
}.to raise_error(RuntimeError)

检查预期错误部分here