Commit 4f57ec16

mo khan <mo@mokhan.ca>
2013-05-05 02:56:45
add seach controller specs
1 parent acb46ba
app/controllers/search_controller.rb
@@ -1,15 +1,11 @@
 class SearchController < ApplicationController
   def index
-    respond_to do |format|
-      if params[:q].blank? 
-        format.html { redirect_to(home_index_url) }
-      else
-        sql = "%"+params[:q]+"%"
-        @creations = Creation.where("upper(name) like upper(?) OR upper(story) like upper(?)", sql, sql).page(params[:page]).per(100)
-        @members = User.where("upper(name) like upper(?)", sql)
-        @search = params[:q]
-        format.html
-      end
+    @search = params[:q]
+    if @search.blank? 
+      redirect_to(home_index_url)
+    else
+      @creations = Creation.search(@search).page(params[:page]).per(100)
+      @members = User.where("upper(name) like upper(?)", "%#{@search}%")
     end
   end
 end
app/models/creation.rb
@@ -38,4 +38,9 @@ class Creation < ActiveRecord::Base
   def add_photo(photo)
     photos.create({:image => photo})
   end
+
+  def self.search(query)
+    sql_search = "%#{query}%"
+    Creation.where("upper(name) like upper(?) OR upper(story) like upper(?)", sql_search, sql_search)
+  end
 end
spec/controllers/profiles_controller_spec.rb
@@ -3,26 +3,31 @@ require 'spec_helper'
 describe ProfilesController do
   include Devise::TestHelpers
 
-  let(:user) { FactoryGirl.build(:user) }
+  let(:user) { FactoryGirl.build(:user, :id => 1002) }
 
   before (:each) do
     request.env['warden'] = mock(Warden, :authenticate => user, :authenticate! => user)
   end
 
   describe "GET 'index'" do
-    it "should be successful" do
+    before :each do
       User.stub(:all){ [user] }
       get 'index'
+    end
+
+    it "should be successful" do
       response.should be_success
     end
   end
 
   describe "GET 'show'" do
-    it "should be successful" do
+    before :each do
       User.stub(:find).with(user.id.to_s){ user }
       get :show, :id => user.id
+    end
+
+    it "should be successful" do
       response.should be_success
     end
   end
-
 end
spec/controllers/search_controller_spec.rb
@@ -1,12 +1,32 @@
 require 'spec_helper'
 
 describe SearchController do
-
   describe "GET 'index'" do
+    let!(:user) { FactoryGirl.create(:user, :name => 'cake') }
+    let!(:bob) { FactoryGirl.create(:user, :name => 'bob') }
+    let!(:cake) { FactoryGirl.create(:creation, :name => 'cake') }
+    let!(:donut) { FactoryGirl.create(:creation, :name => 'donut') }
+
+    before { get :index, { :q => 'cake' } }
+
     it "should be successful" do
-      get 'index', {:q => 'blah'}
       response.should be_success
     end
-  end
 
+    it "should return all creations that have a matching name" do
+      assigns(:creations).should include(cake)
+    end
+
+    it "should not include cakes that do not match" do
+      assigns(:creations).should_not include(donut)
+    end
+
+    it "should return all makers that match" do
+      assigns(:members).should include(user)
+    end
+
+    it "should not include makers with names that do not match" do
+      assigns(:members).should_not include(bob)
+    end
+  end
 end
spec/factories/user.rb
@@ -1,10 +1,9 @@
 FactoryGirl.define do
   factory :user, class: User do
-    id 1002
-    name 'mo'
-    email 'mo@cakeside.com'
+    name { Faker::Name.name }
+    email { Faker::Internet.email }
     password 'password'
-    website 'http://cakeside.com'
+    website { Faker::Internet.http_url }
     city 'calgary'
   end
 end
spec/spec_helper.rb
@@ -6,6 +6,7 @@ require File.expand_path("../../config/environment", __FILE__)
 require 'rspec/rails'
 require 'capybara/rails'
 require 'capybara/rspec'
+require 'ffaker'
 
 Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
 RSpec.configure do |config|
Gemfile
@@ -37,6 +37,7 @@ group :test do
   gem 'fakes-rspec'
   gem 'rb-readline'
   gem 'simplecov'
+  gem 'ffaker'
 end
 group :production do
   gem 'pg'
Gemfile.lock
@@ -128,6 +128,7 @@ GEM
       fakes
       rspec
     fastercsv (1.5.5)
+    ffaker (1.16.1)
     ffi (1.4.0)
     fog (1.10.0)
       builder
@@ -298,6 +299,7 @@ DEPENDENCIES
   dotenv-rails
   factory_girl_rails
   fakes-rspec
+  ffaker
   fog
   formtastic
   jasmine