- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
当模型无法检索记录时,我的应用程序正在将 ActiveRecord::RecordNotFound 传播到 Controller 。
这是数据库查询:
def self.select_intro_verse
offset = rand(IntroVerse.count)
IntroVerse.select('line_one', 'line_two', 'line_three', 'line_four', 'line_five').offset(offset).first!.as_json
end
first!
如果没有找到记录,则引发一个 ActiveRecord::RecordNotFound
,我可以挽救它并在我的 Controller 中呈现一个合适的模板。
这是预期的行为,因此我想在我的规范中对其进行测试。我写道:
context "verses missing in database" do
it "raises an exception" do
expect(VerseSelector.select_intro_verse).to raise_exception
end
end
当我运行 rspec 时:
1) 数据库中缺少 VerseSelector 经文引发异常
失败/错误:expect(VerseSelector.select_intro_verse).to raise_exception
ActiveRecord::RecordNotFound:
ActiveRecord::RecordNotFound
对我来说,即使引发异常,测试也会失败!我怎样才能通过测试?
最佳答案
在文档中查找 rspec-expectations#expecting-errors
这个:
expect(VerseSelector.select_intro_verse).to raise_exception
应该是except
exception 的语法必须是lambda
或proc
:
expect { VerseSelector.select_intro_verse }.to raise_exception(ActiveRecord::RecordNotFound)
关于ruby-on-rails - 测试 ActiveRecord::RecordNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24673532/
我用 rspec 和下一个简单代码测试我的应用程序: require 'spec_helper' describe CarsController do describe "GET 'n
我正在尝试本地化我的 rails 应用程序 (webservice)。 我安装了 gem 'rails-i18n',它工作正常。 除了它不翻译 ActiveRecord::RecordNotFound
问题是当 Restaurant 没有任何符合条件的 MenuItems 时,ActiveRecord 说它找不到 Restaurant .这是相关代码: class Restaurant Time.
在我的app.rb中,我得到了一个'/posts/:id',我已经实现了急救 ActiveRecord:RecordNotFound ,但是它仍然会显示错误,而不是重定向到根URL。是否可以自定义Ac
post = Post.find 666 ActiveRecord::RecordNotFound: Couldn't find Post with 'id'=666 发布 => 无 post = "
当模型无法检索记录时,我的应用程序正在将 ActiveRecord::RecordNotFound 传播到 Controller 。 这是数据库查询: def self.select_intro_ve
我遇到了 this library 的问题因为即使给定的输入不在数据库中,此函数也会返回 false,而实际上它应该返回 true。 type User struct { ID
我是第一次尝试使用 exception_notification。我观看了 Railscast 并遵循了作者在 http://smartinez87.github.io/exception_notif
我正在使用 Rails nested attributes使用 allow_destroy: true。如果我这样调用: deck.update(deck_items_attributes: { id
如何在 Activeadmin 中为我的所有资源拯救 ActiveRecord::RecordNotFound? 我知道在 Rails 中我可以将 rescue_from(ActiveRecord::
我的应用程序 Controller 中有以下代码: class ApplicationController :record_not_found private def record_not_
我有一个带有用户和事件的应用程序。每个用户都有几个事件。当用户想要查看特定事件时,他将执行以下操作: def show begin @userEvents = current_user.e
我有一张表,其中包含模型中的“属于”。 该表包括用于链接两个表的 xx_id 字段。 但是,有时 xx_id 将是空白的。 当它是时,我得到 ActiveRecord::RecordNotFound。
我不太确定在处理数据库中找不到的记录时首选的方法是什么。编写返回 null 的 Find 方法还是编写返回 RecordNotFoundException 的 Get 方法更好? [Authentic
我想在我的 Rails 应用程序中实现 ajax,但收到此错误。 我进行ajax调用的erb代码如下 'showcom
我希望在用户注册时发送电子邮件。此电子邮件应包含一个链接将帐户更改为完整用户。我希望此电子邮件链接成为安全 token 。 email_token 是每个用户随机生成的 token email_act
我正处于开发 (JSON) API 阶段并决定从 ActionController::Metal 继承我的 ApiController 以利用速度等优势。 所以我包含了一堆模块来让它工作。 最近我决定
请参阅问题底部的更新... 作为引用,这个问题是根据我之前在这里遇到的一个问题所做的一些修复演变而来的:Associating Two Models in Rails (user and profil
使用我的 Rails 应用程序,我可以成功创建一个对象(称为工作;将它们视为博客文章)作为 current_user。一个用户 has_many 作品。我可以通过使用我的 postgresql 浏览器
当使用不存在的 id 访问对象的显示页面时,会抛出 RecordNotFonud 异常。有没有办法可以重定向到某个错误页面,或者在抛出此错误时可能采取不同的操作? 最佳答案 如果您使用 Rails 3
我是一名优秀的程序员,十分优秀!