Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #17 from leehambley/improved-specs

Improved specs
  • Loading branch information...
commit 6eb33da0d8ff9367abdbcdabd47fd46bd34e1990 2 parents ce394ed + cbce772
@schneems authored
View
2  Rakefile
@@ -27,3 +27,5 @@ Jeweler::RubygemsDotOrgTasks.new
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
+
+task :default => [:spec]
View
2  lib/likeable.rb
@@ -80,7 +80,7 @@ def likes
def liked_by?(user)
return false unless user
liked_by = @like_user_ids.include?(Likeable.cast_id(user.id)) if @like_user_ids
- liked_by ||= Likeable.redis.hexists(like_key, user.id)
+ liked_by ||= true & Likeable.redis.hexists(like_key, user.id)
end
View
22 spec/likeable/setup_spec.rb
@@ -1,26 +1,6 @@
require 'spec_helper'
-class LikeableIncludedInSetup
-
-end
-
-class SetupCleanTestClassForLikeable
- include Likeable
- def like_key
- "like_key"
- end
-
- def to_hash(*args); {} end
-
- def foo
- end
-
- def id
- @id ||= rand(100)
- end
-end
-
describe Likeable do
describe "setup" do
context "when the User class is defined" do
@@ -28,7 +8,7 @@ def id
reload_user!
Likeable.user_class = User
@user = User.new
- @target = SetupCleanTestClassForLikeable.new
+ @target = CleanTestClassForLikeable.new
end
it "" do
View
56 spec/likeable_spec.rb
@@ -1,33 +1,22 @@
require 'spec_helper'
-class CleanTestClassForLikeable
- include Likeable
- def like_key
- "like_key"
- end
- def to_hash(*args); {} end
+describe Likeable do
- def foo
+ before(:all) do
+ Likeable.setup do |like|
+ like.find_one = lambda { |klass, id| klass.where(:id => id) }
+ end
end
- def id
- @id ||= rand(100)
- end
-end
-
-Likeable.setup do |like|
- like.find_one = lambda {|klass, id| klass.where(:id => id)}
-end
-
-describe Likeable do
-
before(:each) do
@user = User.new
@target = CleanTestClassForLikeable.new
end
+
describe 'instance methods' do
describe "#add_like_from" do
+
it "creates a like" do
target_class = @target.class.to_s.downcase
user_like_key = "users:like:#{@user.id}:#{target_class}"
@@ -37,9 +26,11 @@ def id
Likeable.redis.should_receive(:hset).with(user_like_key, @target.id, time).once
@target.add_like_from(@user, time)
end
+
end
describe "#remove_like_from" do
+
it "removes a like" do
target_class = @target.class.to_s.downcase
user_like_key = "users:like:#{@user.id}:#{target_class}"
@@ -52,14 +43,17 @@ def id
it "doesn't call after_unlike if like didn't exist" do
CleanTestClassForLikeable.after_unlike(:foo)
+ Likeable.redis.should_receive(:hexists).with("like_key", @user.id).and_return(false)
@target = CleanTestClassForLikeable.new
@target.should_not_receive(:foo)
@target.remove_like_from(@user)
end
+
end
describe "#liked_users" do
- it "finds the users that like it" do
+
+ it "finds the users that like it", :integration do
user1 = User.new :name => "user1"
user2 = User.new :name => "user2"
user1.like! @target
@@ -68,7 +62,7 @@ def id
@target.liked_users.should =~ [user1, user2]
end
- it "supports user id models where the id is a hash string" do
+ it "supports user id models where the id is a hash string", :integration do
Likeable.cast_id = lambda { |id| id.to_s }
user_id = "ce7961bd9ca9de6753b6e04754c1c615"
@user.should_receive(:id).at_least(:once).and_return(user_id)
@@ -76,73 +70,91 @@ def id
User.should_receive(:where).with(:id => [user_id]).and_return([@user])
@target.liked_users.should =~ [@user]
end
+
end
describe "#likes" do
+
it "returns set of likes" do
Likeable.redis.should_receive(:hkeys).with("like_key").once
@target.like_user_ids
end
+
end
describe "#liked_by?" do
- it "will answer if current user likes target" do
+
+ it "will answer if current user likes target", :integration do
@target.should_not be_liked_by(@user)
@user.like! @target
@target.should be_liked_by(@user)
end
- it "works with hash string based user ids" do
+ it "works with hash string based user ids", :integration do
user_id = "fa7961bd9ca9de6753b6e04754c1c615"
@user.should_receive(:id).at_least(:once).and_return(user_id)
@target.should_not be_liked_by(@user)
@user.like! @target
@target.should be_liked_by(@user)
end
+
end
describe "#liked_friend_ids" do
+
it "will return all friend ids of user who like target" do
common_value = 3
@target.should_receive(:like_user_ids).and_return([1,2, common_value])
@user.should_receive(:friend_ids).and_return([common_value])
@target.liked_friend_ids(@user).should == [common_value]
end
+
end
describe "#liked_friends" do
+
it "will return all friends who like object" do
values = [1]
@target.should_receive(:liked_friend_ids).with(@user).and_return(values)
User.should_receive(:where).with(:id => values)
@target.liked_friends(@user)
end
+
end
+
end
describe "class methods" do
+
describe 'after_like' do
+
it 'should be a class method when included' do
CleanTestClassForLikeable.respond_to?(:after_like).should be_true
end
+
it 'is called after a like is created' do
CleanTestClassForLikeable.after_like(:foo)
@target.should_receive(:foo)
@target.add_like_from(@user)
end
+
end
describe 'after_unlike' do
+
it 'should be a class method when included' do
CleanTestClassForLikeable.respond_to?(:after_unlike).should be_true
end
+
it 'is called after a like is destroyed' do
CleanTestClassForLikeable.after_unlike(:foo)
Likeable.redis.should_receive(:hexists).and_return(true)
@target.should_receive(:foo)
@target.remove_like_from(@user)
end
+
end
+
end
end
View
173 spec/spec_helper.rb
@@ -1,85 +1,150 @@
require 'rubygems'
require 'active_record'
-
+require 'singleton'
+require 'tempfile'
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../..', 'lib'))
-
require 'likeable'
+module UserHelperMethods
-def build_user!
- eval %Q{
- class User
- include Likeable::UserMethods
- def id
- @time ||= Time.now.to_f.to_s.tr('.', '').to_i
- end
+ def build_user!
+ eval %Q{
+ class ::User
+ include Likeable::UserMethods
+ def id
+ @time ||= Time.now.to_f.to_s.tr('.', '').to_i
+ end
- def self.where(*args)
- end
+ def self.where(*args)
+ end
- def friends_ids
- []
- end
+ def friends_ids
+ []
+ end
- def self.after_destroy
+ def self.after_destroy
+ end
end
- end
- }
-end
+ }
+ end
+
+ def unload_user!
+ Object.instance_eval{ remove_const :User }
+ end
+
+ def reload_user!
+ unload_user!
+ build_user!
+ end
+
+ def default_adapter!
+ Likeable.adapter = Likeable::DefaultAdapter
+ end
-def unload_user!
- Object.instance_eval{ remove_const :User }
end
-def reload_user!
- unload_user!
- build_user!
+RSpec.configure do |c|
+
+ c.include UserHelperMethods
+
+ c.treat_symbols_as_metadata_keys_with_true_values = true
+
+ c.before :suite do
+ Likeable.redis = NullRedis.new
+ end
+
+ c.before :each do
+ build_user!
+ end
+
+ c.around :each, :integration do |example|
+ IntegrationTestRedis.instance.start
+ Likeable.redis = IntegrationTestRedis.instance.client
+ Likeable.redis.flushdb
+ example.run
+ IntegrationTestRedis.instance.stop
+ Likeable.redis = NullRedis.new
+ end
+
end
-def default_adapter!
- Likeable.adapter = Likeable::DefaultAdapter
+class NullRedis
+
+ def method_missing(*args)
+ self
+ end
+
+ def to_s
+ "Null Redis"
+ end
+
end
-build_user!
+class IntegrationTestRedis
+ include ::Singleton
-require 'tempfile'
-require 'rspec'
-require 'rspec/autorun'
+ PORT = 9737
+ PIDFILE = Tempfile.new('likeable-integration-test-redis-pid')
+
+ def start
+ install_at_exit_handler
+ system("echo '#{options}' | redis-server -")
+ end
-RSpec.configure do |config|
- REDIS_PIDFILE = Tempfile.new("likeable-redis")
+ def stop
+ system("if [ -e #{PIDFILE.path} ]; then kill -QUIT $(cat #{PIDFILE.path}) 2>/dev/null; fi")
+ end
+
+ def client
+ return Redis.new(:port => PORT, :db => 15)
+ end
+
+ private
+
+ def options
+ {
+ 'daemonize' => 'yes',
+ 'pidfile' => PIDFILE.path,
+ 'bind' => '127.0.0.1',
+ 'port' => PORT,
+ 'timeout' => 300,
+ 'dir' => '/tmp',
+ 'loglevel' => 'debug',
+ 'logfile' => 'stdout',
+ 'databases' => 16
+ }.map { |k, v| "#{k} #{v}" }.join("\n")
+ end
+
+ def install_at_exit_handler
+ at_exit {
+ IntegrationTestRedis.instance.stop
+ }
+ end
+
+end
+
+class CleanTestClassForLikeable
- config.before(:suite) do
- redis_options = {
- "daemonize" => 'yes',
- "pidfile" => REDIS_PIDFILE.path,
- "bind" => "127.0.0.1",
- "port" => 9737,
- "timeout" => 300,
- "dir" => "/tmp",
- "loglevel" => "debug",
- "logfile" => "stdout",
- "databases" => 16
- }.map { |k, v| "#{k} #{v}" }.join('\n')
- `echo '#{redis_options}' | redis-server -`
+ include Likeable
- $redis_test_connection = Redis.new(:port => 9737, :db => 15)
+ def like_key
+ "like_key"
end
- config.before(:each) do
- default_adapter!
- $redis_test_connection.flushdb
- Redis.stub(:new).and_return($redis_test_connection)
+ def to_hash(*args);
+ Hash.new
end
- # TODO this needs to handle premature exits, too
- config.after(:suite) do
- %x{
- cat #{REDIS_PIDFILE.path} | xargs kill -QUIT
- }
+ def foo
+ nil
end
+
+ def id
+ @id ||= rand(100)
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.