Skip to content

Commit

Permalink
add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
i2bskn committed Jul 13, 2013
1 parent a873d31 commit 8844799
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 10 deletions.
1 change: 0 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def tag_labels

def update_with_omniauth(auth)
update_attributes!(
uid: auth[:uid],
nickname: auth[:info][:nickname],
email: auth[:info][:email],
image_url: auth[:info][:image],
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/secret_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

# Make sure your secret_key_base is kept private
# if you're sharing your code publicly.
TaggedGist::Application.config.secret_key_base = '1403ff6447012fb7ef1cc53d21a36935171f132130489d4d457cea155197005dbdf1f0c42507f8d73c5c3bcd2d179426f87e0c1d7ef4a8b52f79e23368e6305a'
TaggedGist::Application.config.secret_key_base = ENV['RAILS_SECRET_KEY']
63 changes: 59 additions & 4 deletions spec/controllers/root_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,66 @@
require 'spec_helper'

describe RootController do
let(:user) {FactoryGirl.create(:user)}

describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
def valid_session
{user: user.id}
end

describe "GET index" do
context "with not sign in" do
it "returns http success" do
get :index
expect(response).to be_success
end

it "should not called User#tag_labels" do
User.any_instance.should_not_receive(:tag_labels)
get :index
end
end

context "with sign in" do
let!(:tags) do
tags = ["ruby", "javascript"]
tags.each_with_index do |tag, i|
g = Gist.new(gid: "#{Time.now.usec}#{i}", user_id: user.id)
Tag.create!(name: tag, user_id: user.id, gist_id: g.id)
end
tags
end

it "return http success" do
get :index, {}, valid_session
expect(response).to be_success
end

it "assigns tags as all tag labels" do
get :index, {}, valid_session
expect(assigns(:tags)).to eq(tags)
end

it "assigns gists as all gists" do
get :index, {}, valid_session
expect(assigns(:gists)).to eq(user.gists)
end

it "assigns gists as specified tag" do
get :index, {tag: "ruby"}, valid_session
assigns(:gists).each do |g|
expect(g.tags.inject(false){|f,t| f = true if t.name == "ruby"}).to be_true
end
end

it "assigns current_tag as nil" do
get :index, {}, valid_session
expect(assigns(:current_tag)).to be_nil
end

it "assigns current_tag as specified tag" do
get :index, {tag: "ruby"}, valid_session
expect(assigns(:current_tag)).not_to be_nil
end
end
end

Expand Down
79 changes: 75 additions & 4 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ def valid_auth
uid: "12345678",
info: {
nickname: "example",
email: "MyString",
image: "MyString",
email: "user@example.com",
image: "image_url",
urls: {
GitHub: "MyString"
GitHub: "github_url"
}
},
credentials: {
token: "MyString"
token: "oauth_token"
}
}
end
Expand All @@ -33,4 +33,75 @@ def valid_auth
expect{User.create_with_omniauth(nil)}.to raise_error(NoMethodError)
end
end

describe "#tag_labels" do
let!(:user) do
user = FactoryGirl.create(:user)
["ruby", "perl", "php", "python", "java", "c"].each_with_index do |t,i|
2.times do |x|
g = Gist.create!(
gid: "#{x.to_s}#{i.to_s}",
description: "MyString",
html_url: "MyString",
embed_url: "MyString",
public_gist: true,
user_id: user.id
)
Tag.create!(name: t, user_id: user.id, gist_id: g.id)
end
end
user
end

it "should get labels" do
expect(user.tag_labels).to eq(["ruby", "perl", "php", "python", "java", "c"])
end

it "should return empty array if user don't have tags" do
user = FactoryGirl.create(:user)
expect(user.tag_labels).to be_empty
end
end

describe "#update_with_omniauth" do
let!(:user) {FactoryGirl.create(:user)}

it "nickname should be updated" do
expect {
user.update_with_omniauth(valid_auth)
}.to change(user, :nickname)
end

it "email should be updated" do
expect {
user.update_with_omniauth(valid_auth)
}.to change(user, :email)
end

it "image_url should be updated" do
expect {
user.update_with_omniauth(valid_auth)
}.to change(user, :image_url)
end

it "github_url should be updated" do
expect {
user.update_with_omniauth(valid_auth)
}.to change(user, :github_url)
end

it "access_token should be updated" do
expect {
user.update_with_omniauth(valid_auth)
}.to change(user, :access_token)
end

it "exception should be generated if not arguments" do
expect{user.update_with_omniauth}.to raise_error(ArgumentError)
end

it "exception should be generated if argument is nil" do
expect{user.update_with_omniauth(nil)}.to raise_error(NoMethodError)
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
add_filter "/vendor/"
end

ENV['RAILS_SECRET_KEY'] = "5ebe2294ecd0e0f08eab7690d2a6ee69"

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
Expand Down

0 comments on commit 8844799

Please sign in to comment.