【问题标题】:Do we need to test callback in ActiveRecord like after_destroy?我们是否需要像 after_destroy 一样在 ActiveRecord 中测试回调?
【发布时间】:2013-12-25 06:37:17
【问题描述】:

我们是否需要像after_destroy一样在ActiveRecord中测试回调?

【问题讨论】:

    标签: ruby-on-rails unit-testing tdd


    【解决方案1】:

    鉴于这样的事情:

    after_destroy :clean_up_some_stuff
    

    我认为您不需要测试 after_destroy 是否有效,但您确实需要全面测试 clean_up_some_stuff 方法是否正常工作。

    【讨论】:

      【解决方案2】:

      你可以在这里测试两件事。

      你的方法在销毁clean_up_some_stuff之后调用的目的是什么

      test "should do the clean up stuff" do
        assert Model.new.clean_up_some_stuff
        # more asserts to verify the job is done
      end
      

      销毁对象并验证回调是否成功

      test "should destroy" do
        object = Model.create
        assert  object.destroy
        # more asserts to verify the job is done
      end
      

      assertions-cheat-sheet 应该会有所帮助。

      【讨论】:

        猜你喜欢
        • 2021-11-16
        • 2017-09-30
        • 1970-01-01
        • 2019-07-21
        • 2011-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        相关资源
        最近更新 更多