Fake Cookie Store for Rails Unit Tests
While writing tests for an AB testing library, I needed to simulate a Rails cookie store, but I didn't want to do any controller requests. Without doing a controller request, you aren't given any cookie store to test with, so I whipped up this fake cookie store that seems to work pretty well.
class FakeCookieStore < Hash
# allow for things like cookies.permanent.signed etc
def method_missing(name, *args, &block); self; end
def []=(key, value)
# simulate cookies[key] = {:value => 'foo'}
if value.is_a?(Hash) && (v = value[:value])
super(key, v)
else
super
end
end
end
You should
follow me
on Twitter.
Comments
blog comments powered by Disqus