- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个模型 Lead 和一个回调:after_commit :create, :send_to_SPL
我正在使用 Rails-4.1.0、ruby-2.1.1、RSpec。
1) 此规范未通过:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(lead).to receive(:send_to_SPL)
lead = Lead.create(init_hash)
p lead.new_record? # => false
end
end
2) 这个规范也没有通过:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(ActiveSupport::Callbacks::Callback).to receive(:build)
lead = Lead.create(init_hash)
end
end
3) 这个通过了,但我认为它没有测试 after_commit 回调:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(lead).to receive(:send_to_SPL)
lead.send(:send_to_SPL)
end
end
在 Rails 中测试 after_commit 回调的最佳方法是什么?
最佳答案
我以为Mihail Davydenkov's comment当之无愧的答案:
You can also use
subject.run_callbacks(:commit)
.
另请注意,此问题(在事务测试中未调用提交回调)应在 rails 5.0+ 中得到修复,因此您可能希望在升级期间删除可能使用的任何解决方法。请参阅:https://github.com/rails/rails/pull/18458
关于ruby-on-rails - 使用 RSpec 和模拟测试 after_commit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988968/
有没有人实现过after_commit卡在 Rails 中?我不是在更新/创建/等提交后寻找基于模型的模型,我希望能够动态定义一个只有在当前(最顶层)事务通过时才会执行的 block : def re
更新: 对 update_attributes 的调用是否会获得它自己的事务? 我看过 this问题以及除该问题之外的其他原因,我决定将 after_commit 作为正确的钩子(Hook)。问题是它
我在 Rails 3.0.8 上并尝试使用 after_commit 回调。 它在这里定义:https://github.com/rails/rails/blob/v3.0.8/activerecor
我在我的应用程序中使用了 after_commit。 我希望它仅在我的模型中更新特定字段时触发。有人知道怎么做吗? 最佳答案 老问题,但这是我发现的一种方法,可以与 after_commit 回调一起
我的 after_commit 设置如下。 class MyModel < ActiveRecord::Base after_commit { Rails.logger.info ("commit
我已经定义了after_commit回调 update .它不会在 rspec 中触发。 这是我的回调: after_commit :notify_trip, :if => Proc.new { |t
我有一个需要在提交后运行的 sidekiq 作业,但仅在某些情况下而不是所有情况下,以避免 a common race condition . 例如,下面的 after_commit将始终触发,但内部
当我需要在 after_commit, :on => :create 期间更新属性时,我创建了一个无限回调循环.但是,仅当我需要在此回调期间更新对象的属性时才会发生这种情况。有没有办法防止这种情况?我
如果我在 Rails 中有多个相同性质的回调,会不会有冲突?像同一模型的几个 after_commit ? 我想我可以将它们加入一种方法中,但出于可读性原因,我宁愿不.. 最佳答案 不,他们会一个接一
有没有办法在创建记录时跟踪对 after_commit 模型的更改?我尝试过使用脏模块,并且能够在更新记录时跟踪更改,但是在创建记录时不会记录更改。 最佳答案 您不能使用 rails changed?
对于特定的数据库表,我们需要一个始终与数据库同步的内存缓存。我目前的尝试是在 after_commit Hook 中将更改写入缓存 - 这样我们确保不会将任何更改写入缓存,以后可能会恢复。 但是,这种
我想在 destroy 一个对象时跳过 after_commit 回调,而不是使用 on 语法明确指定方法列表. IE。像这样的东西: after_commit :foo, except: [:des
我有以下片段: class Product after_commit :do_something, on: %i(update create) def do_something if # u
我有一个模型 Lead 和一个回调:after_commit :create, :send_to_SPL 我正在使用 Rails-4.1.0、ruby-2.1.1、RSpec。 1) 此规范未通过:
after_save在保存记录时调用(但在提交之前)。同时 after_create在提交数据库事务之后调用。 除了这篇文章http://www.justinweiss.com/articles/a-
我听说 rails 有一个脏/更改标志。是否可以在 after_commit 回调中使用它? 在我的用户模型中,我有: after_commit :push_changes 在 def push_ch
设计激活后我遇到了奇怪的问题。带有回形针属性 has_mongoid_attached_file 的模型返回异常: undefined method `after_commit' for Item:C
我正在尝试使用我的后台任务管理器处理一些竞争情况。本质上,我有一个 Thing 对象(已经存在)并为其分配一些属性,然后保存它。使用新属性保存后,我将其放入 Resque 中,并传入 ID。 thin
我经历了 rails 中 after_save 和 after_commit 回调的差异,我发现的主要差异是 after_commit 在创建和更新时调用,有没有办法只在创建时调用 after_com
我正在以这种方式创建一个 ActiveRecord 事务 ActiveRecord::Base.transaction do MyModel.create!(name: "value") Ot
我是一名优秀的程序员,十分优秀!