diff options
author | re4k <re4k@re4k.info> | 2013-04-24 22:08:52 +0900 |
---|---|---|
committer | re4k <re4k@re4k.info> | 2013-04-24 22:08:52 +0900 |
commit | 102abd81a184576b3ba2880db6440e0dd8b5fde7 (patch) | |
tree | fce2d37723361b82917423eb754a9b50e03defff /spec | |
parent | 6ff657ed5f1d39f24a08ea599cacae61dc7b59da (diff) | |
download | aclog-102abd81a184576b3ba2880db6440e0dd8b5fde7.tar.gz |
add User model tests
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/users.rb | 8 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 140 |
2 files changed, 147 insertions, 1 deletions
diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 87159e0..f5a9675 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -8,6 +8,14 @@ FactoryGirl.define do protected false end + factory :user_2, class: User do + id 1326331596 + screen_name "aclog_test" # same + name "aclog test (new)" + profile_image_url "https://si0.twimg.com/sticky/default_profile_images/default_profile_3_normal.png" + protected true + end + # screen_name won't be changed factory :user_exists, class: User do id 15926668 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 44032b4..748ad97 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,5 +1,143 @@ require 'spec_helper' describe User do - pending "add some examples to (or delete) #{__FILE__}" + describe ".cached" do + let(:user) { FactoryGirl.create(:user_1) } + + shared_examples_for "cached_exists" do + its(:id) { should be user.id } + its(:screen_name) { should eq user.screen_name } + its(:name) { should eq user.name } + its(:profile_image_url) { should eq user.profile_image_url } + its(:protected) { should be user.protected } + end + + context "when exists" do + describe "user_id" do + subject { User.cached(user.id) } + it_behaves_like("cached_exists") + end + + context "screen_name" do + subject { User.cached(user.screen_name) } + it_behaves_like("cached_exists") + end + end + + context "when not exists" do + describe "user_id" do + subject { User.cached(-1) } + it { should be nil } + end + + context "screen_name" do + subject { User.cached("me") } + it { should be nil } + end + end + end + + describe ".from_user_object" do + context "when not recorded" do + let(:user_1_model) { FactoryGirl.build(:user_1) } + let(:user_object) { OpenStruct.new(id: user_1_model.id, + screen_name: user_1_model.screen_name, + name: user_1_model.name, + profile_image_url_https: user_1_model.profile_image_url, + protected: user_1_model.protected) } + subject { User.from_user_object(user_object) } + its(:id) { should be user_object.id } + its(:screen_name) { should eq user_object.screen_name } + its(:name) { should eq user_object.name } + its(:profile_image_url) { should eq user_object.profile_image_url_https } + its(:protected) { should be user_object.protected } + end + + context "when already recorded" do + let!(:user_1) { FactoryGirl.create(:user_1) } + let(:user_2_model) { FactoryGirl.build(:user_2) } + let(:user_object) { OpenStruct.new(id: user_2_model.id, + screen_name: user_2_model.screen_name, + name: user_2_model.name, + profile_image_url_https: user_2_model.profile_image_url, + protected: user_2_model.protected) } + subject { User.from_user_object(user_object) } + its(:id) { should be user_object.id } + its(:id) { should be user_1.id } + its(:screen_name) { should eq user_object.screen_name } + its(:screen_name) { should eq user_1.screen_name } + its(:name) { should eq user_object.name } + its(:profile_image_url) { should eq user_object.profile_image_url_https } + its(:protected) { should be user_object.protected } + end + end + + describe ".delete_cache" do + # TODO + end + + describe "#protected?" do + context "when not protected" do + let(:user) { FactoryGirl.create(:user, protected: false) } + subject { user } + its(:protected?) { should be false } + end + + context "when protected" do + let(:user) { FactoryGirl.create(:user, protected: true) } + subject { user } + its(:protected?) { should be true } + end + end + + describe "#account" do + context "when exists" do + let!(:account) { FactoryGirl.create(:account_1) } + let(:user) { FactoryGirl.create(:user_1) } + subject { user.account } + it { should eq account } + end + + context "when not exists" do + let!(:account) { FactoryGirl.create(:account_1) } + let(:user) { FactoryGirl.create(:user_exists) } + subject { user.account } + it { should be nil } + end + end + + describe "#profile_image_url_original" do + let(:user) { FactoryGirl.create(:user) } + before { user.stub!(:profile_image_url).and_return("https://example.com/profile_image_normal.png") } + subject { user.profile_image_url_original } + it { should eq "https://example.com/profile_image.png" } + end + + describe "#stats" do + let!(:account) { FactoryGirl.create(:account_1) } + let(:user) { FactoryGirl.create(:user_1) } + let(:stats_api) { {favorites_count: 10, + listed_count: 12, + followers_count: 14, + tweets_count: 16, + friends_count: 18, + bio: "abc"} } + before do + user_2, user_3 = FactoryGirl.create_list(:user, 2) + tweet_1, tweet_2 = FactoryGirl.create_list(:tweet, 2, user: user) + FactoryGirl.create(:favorite, tweet: tweet_1, user: user_2) + FactoryGirl.create(:favorite, tweet: tweet_1, user: user_3) + FactoryGirl.create(:retweet, tweet: tweet_2, user: user_2) + + account.stub!(:stats_api).and_return(stats_api) + user.stub!(:account).and_return(account) + end + subject { OpenStruct.new(user.stats) } + its(:stats_api) { should eq stats_api } + its(:favorites_count) { should be 0 } + its(:retweets_count) { should be 0 } + its(:tweets_count) { should be 2 } + its(:favorited_count) { should be 2 } + its(:retweeted_count) { should be 1 } + end end |