flash.now在正常情况下当 action结束时会被清理,所以在测试中无法获取它,如何在RSpec中获取flash.now,我们需要通过重写flash来得到,在spec_helper .rb文件中加入一下代码就可以在测试中测试flash.now:
module ActionController
  module Flash
    class FlashHash
      def initialize
        @hash = {}
        @now_hash = {}
      end
      
      def [](key)
        @hash[key]
      end
      
      def []=(key, obj)
        @hash[key] = obj
      end
      
      def discard(k = nil)
        initialize
      end
      
      def now
        @now_hash
      end
      
      def update(hash)
        @hash.update(hash)
      end
      
      def sweep
        # do nothing
      end
    end
  end
end
评论
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论