Skip to content
Permalink
Browse files

Fix test

  • Loading branch information...
angela4713
angela4713 committed Aug 17, 2015
1 parent 0fdca45 commit d4219763c6f0cdfafc0d36b9d814d2e995d40319
Showing with 11 additions and 7 deletions.
  1. +2 −2 app/controllers/sessions_controller.rb
  2. +7 −3 app/models/user.rb
  3. +2 −2 test/models/user_test.rb
@@ -10,11 +10,11 @@ def create
flash[:success] = 'Validation failed'
redirect_to :root
else
gems = Gems.gems(user.username)
user.sync_gems(gems)
session[:user_id] = user.id
flash[:success] = 'Signed in!'
redirect_to :gems
gems = Gems.gems(current_user.username)
current_user.sync_gems(gems)
end
end

@@ -7,9 +7,13 @@ class User < ActiveRecord::Base
def sync_gems(gems)
ruby_gem_array = Array.new
gems.each do |g|
ruby_gem = RubyGem.find_or_create_by(name: g['name'], info: g['info'], project_uri: g['project_uri'])
ruby_gem_array << ruby_gem.id
self.ruby_gems << ruby_gem unless self.ruby_gems.exists?(ruby_gem)
ruby_gem = RubyGem.find_or_initialize_by(name: g['name'])
if ruby_gem.new_record?
ruby_gem.assign_attributes(info: g['info'], project_uri: g['project_uri'])
ruby_gem.save!
ruby_gem_array << ruby_gem.id
self.ruby_gems << ruby_gem unless self.ruby_gems.exists?(ruby_gem)
end
end
ruby_gems = self.ruby_gems.where('ruby_gems_users.ruby_gem_id NOT IN (?)', ruby_gem_array)
self.ruby_gems.destroy(ruby_gems)
@@ -19,15 +19,15 @@ class UserTest < ActiveSupport::TestCase
end
end

test "should store user new gems" do
test "should remove gems that do not belongs to the user anymore" do
response = [{"name"=>"play_hangman", "info"=>"Play Hangman on your console! Install, run the command 'play_hangman' and enjoy.\nThis game was built using the 'hangman_engine' gem. Check it out to build your own Hangman game.", "project_uri"=>"https://rubygems.org/gems/play_hangman"}]
user = users(:angela)
assert_difference('user.ruby_gems.count', -2) do
user.sync_gems(response)
end
end

test "should remove gems that do not belongs to the user anymore" do
test "should store user new gems" do
response = [{"name"=>"rails", "info"=>"Full-stack web framework optimized for programmer happiness", "project_uri"=>"https://rubygems.org/gems/rails"}, {"name"=>"faraday", "info"=>"HTTP/REST API client library.", "project_uri"=>"https://rubygems.org/gems/faraday"}, {"name"=>"pg", "info"=>"Pg is the Ruby interface to the PostgreSQL", "project_uri"=>"https://rubygems.org/gems/pg"}, {"name"=>"play_hangman", "info"=>"Play Hangman on your console! Install, run the command 'play_hangman' and enjoy.\nThis game was built using the 'hangman_engine' gem. Check it out to build your own Hangman game.", "project_uri"=>"https://rubygems.org/gems/play_hangman"}]
user = users(:angela)
assert_difference('user.ruby_gems.count', 1) do

0 comments on commit d421976

Please sign in to comment.
You can’t perform that action at this time.