ruby-on-rails - rspec 测试中预计会出现错误

标签 ruby-on-rails rspec

我试图预期 rspec 测试中出现错误。

lambda {Participant.create!({:user_id => three.id, :match_id => match.id, :team => 1})}.should raise_error StandardError

现在我只是使用 StandardError 来确保它正常工作。

1) StandardError in 'Participant should never allow more participants than players'. 
   This game is already full.  Cannot add another player.
/home/josiah/Projects/Set-Match/app/models/participant.rb:12:in `do_not_exceed_player_count_in_match'
./spec/models/participant_spec.rb:24:

它显然抛出了错误,但我的测试仍然失败。

想法?

最佳答案

有一段时间了,但至少在 RSpec 2.5 中,可以使用

expect {raise 'boom'}.to raise_error(RuntimeError, /boom/)

关于ruby-on-rails - rspec 测试中预计会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2359439/

相关文章:

ruby-on-rails - Rails 路由 + AngularJS 路由

ruby-on-rails - 测试 Rails FormBuilder 扩展

ruby-on-rails - 为什么在使用 RSpec 的共享示例时我的局部变量未定义?

ruby-on-rails - rails : Cleaning up large methods in controllers

jquery - div 元素之间的空间随着每个新的 div 呈指数级增长

python - 在 Ruby 代码中放置单个断点

ruby-on-rails - ruby on rails 设计模式

ruby-on-rails - 在 rspec 中更改关联对象的正确方法

ruby-on-rails - 使用 Rspec GET 发送格式

ruby-on-rails - DatabaseCleaner 引发 NoMethodError : undefined method `rollback' for nil:NilClass