flash.now在正常情况下当 action结束时会被清理,所以在测试中无法获取它,如何在RSpec中获取flash.now,我们需要通过重写flash来得到,在spec_helper .rb文件中加入一下代码就可以在测试中测试flash.now:
[code]
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
[/code]
[code]
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
[/code]