Mike Ferrier

I beat code into submission.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
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

Comments