discourse/spec/components/cache_spec.rb
Andy Waite 3e50313fdc Prepare for separation of RSpec helper files
Since rspec-rails 3, the default installation creates two helper files:
* `spec_helper.rb`
* `rails_helper.rb`

`spec_helper.rb` is intended as a way of running specs that do not
require Rails, whereas `rails_helper.rb` loads Rails (as Discourse's
current `spec_helper.rb` does).

For more information:

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

In this commit, I've simply replaced all instances of `spec_helper` with
`rails_helper`, and renamed the original `spec_helper.rb`.

This brings the Discourse project closer to the standard usage of RSpec
in a Rails app.

At present, every spec relies on loading Rails, but there are likely
many that don't need to. In a future pull request, I hope to introduce a
separate, minimal `spec_helper.rb` which can be used in tests which
don't rely on Rails.
2015-12-01 20:39:42 +00:00

77 lines
1.4 KiB
Ruby

require 'rails_helper'
require 'cache'
describe Cache do
let :cache do
Cache.new
end
it "supports fixnum" do
cache.write("num", 1)
expect(cache.read("num")).to eq(1)
end
it "supports hash" do
hash = {a: 1, b: [1,2,3]}
cache.write("hash", hash)
expect(cache.read("hash")).to eq(hash)
end
it "can be cleared" do
$redis.set("boo", "boo")
cache.write("hello0", "world")
cache.write("hello1", "world")
cache.clear
expect($redis.get("boo")).to eq("boo")
expect(cache.read("hello0")).to eq(nil)
end
it "can delete correctly" do
cache.fetch("key", expires_in: 1.minute) do
"test"
end
cache.delete("key")
expect(cache.fetch("key")).to eq(nil)
end
it "calls setex in redis" do
cache.delete("key")
cache.delete("bla")
key = cache.namespaced_key("key")
cache.fetch("key", expires_in: 1.minute) do
"bob"
end
expect($redis.ttl(key)).to be_within(2.seconds).of(1.minute)
# we always expire withing a day
cache.fetch("bla"){ "hi" }
key = cache.namespaced_key("bla")
expect($redis.ttl(key)).to be_within(2.seconds).of(1.day)
end
it "can store and fetch correctly" do
cache.delete "key"
r = cache.fetch "key" do
"bob"
end
expect(r).to eq("bob")
end
it "can fetch existing correctly" do
cache.write "key", "bill"
r = cache.fetch "key" do
"bob"
end
expect(r).to eq("bill")
end
end