rails

Wisper在测试中可能会出现的问题

By jere on 2016-04-18 0 Comments

项目使用了wisper, 也使用了wisper-activerecord. 在测试中发现,model在save时,死活不会触发broadcast(:create_model_successful)或者broadcast(:update_model_successful). 但在开发环境中就是好的,在console的环境里也是好的。

看wisper源码,看wisper-activerecord源码,最终才找到了答案,答案是什么呢?我们先看它是如何实现的:

module Wisper
module ActiveRecord
module Publisher
extend ActiveSupport::Concern
included do

include Wisper::Publisher

after_validation :after_validation_broadcast
after_commit :after_create_broadcast, on: :create
after_commit :after_update_broadcast, on: :update
after_commit :after_destroy_broadcast, on: :destroy
after_commit :after_commit_broadcast
after_rollback :after_rollback_broadcast
end
end

是使用了after_commit的机制,其时问题就成为了after_commit在测试下不工作

我们再看cucubmer 与rspec的设置,是transactional的,问题就出在这里,测试代码就不commit,你让事件怎么触发?也真是的,开始的时候就没有想到这一点,浪费了半天的时间找探索。

解决方案也简单,去这个gem上看:test-after-commit.

打赏

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用*标注