3

我写了一个测试,我希望结果会引发错误。

it{ Authentication.create_with_omniauth!(nil, nil).should raise_error }

结果确实会抛出错误,但验证失败。显然它正在抛出一个错误。:)

2) Authentication methods: self.create_with_omniauth!: with bad input 
     Failure/Error: it{ Authentication.create_with_omniauth!(nil, nil).should raise_error }
     ArgumentError:
       ArgumentError
     # ./app/models/authentication.rb:13:in `create_with_omniauth!'
     # ./spec/models/authentication_spec.rb:69:in `block (5 levels) in <top (required)>'

怎么办?你如何测试错误?

4

2 回答 2

3

您应该使用expect {}.to raise_error语法

expect { Authentication.create_with_omniauth!(nil, nil) }.to raise_error

或者

lambda { Authentication.create_with_omniauth!(nil, nil) }.should raise_error

如果您使用 Rspec 1.XX

查看文档

https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/raise-error-matcher#expect-any-error

http://rspec.rubyforge.org/rspec/1.2.9/classes/Spec/Matchers.html#M000176

于 2012-12-26T17:25:43.143 回答
0

我没有使用omniauth的经验,但是这样的事情有用吗?

describe "authentication" do
  context "when nil" do
    let(:nil_authentication) { Authentication.create_with_omniauth!(nil, nil) }
    subject { -> { nil_authentication } }
    it { should raise_error }
  end
end
于 2012-12-27T14:23:10.230 回答