Commit 99d1c74c

mo khan <mo@mokhan.ca>
2015-01-02 17:45:28
upgrade specs to expect syntax.
1 parent 7cbeda8
spec/controllers/admin/products_controller_spec.rb
@@ -12,13 +12,13 @@ module Admin
       let(:product_api) { double(find: true) }
 
       before :each do
-        controller.stub(:product_api).and_return(product_api)
+        allow(controller).to receive(:product_api).and_return(product_api)
       end
 
       it 'loads the product details from amazon' do
         asin = 'asin'
         product = "product"
-        product_api.stub(:find).with(asin).and_return(product)
+        allow(product_api).to receive(:find).with(asin).and_return(product)
 
         get :show, id: asin
 
spec/controllers/admin/sessions_controller_spec.rb
@@ -22,8 +22,8 @@ module Admin
         let(:user_session) { create(:active_session) }
 
         before :each do
-          UserSession.stub(:find).with(user_session.id).and_return(user_session)
-          user_session.stub(:revoke!).and_return(true)
+          allow(UserSession).to receive(:find).with(user_session.id).and_return(user_session)
+          allow(user_session).to receive(:revoke!).and_return(true)
           delete :destroy, id: user_session.id
         end
 
spec/controllers/admin/users_controller_spec.rb
@@ -19,8 +19,8 @@ module Admin
       it "returns users that match the search results" do
         matching_user = double
         repository = double
-        controller.stub(:repository).and_return(repository)
-        repository.stub(:search_with).and_return([matching_user])
+        allow(controller).to receive(:repository).and_return(repository)
+        allow(repository).to receive(:search_with).and_return([matching_user])
         get :index, q: 'mo'
         expect(assigns(:users)).to include(matching_user)
       end
spec/controllers/api/v1/cakes_controller_spec.rb
@@ -13,11 +13,11 @@ describe Api::V1::CakesController do
     end
 
     it "should return all of my cakes" do
-      assigns(:cakes).should include(my_cake)
+      expect(assigns(:cakes)).to match_array([my_cake])
     end
 
     it "should not return any other cakes" do
-      assigns(:cakes).should_not include(other_cake)
+      expect(assigns(:cakes)).to_not include(other_cake)
     end
   end
 end
spec/controllers/my/dashboard_controller_spec.rb
@@ -11,7 +11,7 @@ describe My::DashboardController do
 
     it "loads the most recent activities" do
       get :index
-      assigns(:activities).should include(activity)
+      expect(assigns(:activities)).to match_array([activity])
     end
   end
 end
spec/controllers/my/favorites_controller_spec.rb
@@ -12,7 +12,7 @@ describe My::FavoritesController do
     end
 
     it "loads all my favorite cakes" do
-      assigns(:creations).should include(favorite_cake)
+      expect(assigns(:creations)).to include(favorite_cake)
     end
   end
 end
spec/controllers/my/passwords_controller_spec.rb
@@ -6,8 +6,8 @@ describe My::PasswordsController do
       let(:user) { create(:user) }
 
       it "should redirect you to the login page" do
-        put :update, :id => user.id
-        response.should redirect_to(login_path)
+        put :update, id: user.id
+        expect(response).to redirect_to(login_path)
       end
     end
 
@@ -20,11 +20,11 @@ describe My::PasswordsController do
         before { put :update, :id => user.id, :user => { :password => 'foobar', :password_confirmation => 'barfoo' } }
 
         it "should display an error on the page" do
-          flash[:error].should == I18n.t(:passwords_do_not_match)
+          expect(flash[:error]).to eql(I18n.t(:passwords_do_not_match))
         end
 
         it "should render the show template" do
-          response.should render_template(:index)
+          expect(response).to render_template(:index)
         end
       end
 
@@ -40,7 +40,6 @@ describe My::PasswordsController do
         end
       end
     end
-
   end
 
   describe "#index" do
@@ -54,10 +53,9 @@ describe My::PasswordsController do
 
       context "when displaying a form to change the current password" do
         it "should load the user" do
-          assigns(:user).should == user
+          expect(assigns(:user)).to eql(user)
         end
       end
-
     end
   end
 end
spec/controllers/my/settings_controller_spec.rb
@@ -6,7 +6,7 @@ describe My::SettingsController do
       user = create(:user)
       http_login(user)
       get :index
-      assigns(:user).should == user
+      expect(assigns(:user)).to eql(user)
     end
   end
 
@@ -16,24 +16,24 @@ describe My::SettingsController do
     before :each do
       http_login(user)
       patch :update, id: user.id, user: { name: 'mo khan', email: 'mo@mokhan.ca', city: 'Calgary', website: 'http://mokhan.ca/', twitter: 'mocheen', facebook: 'fb' }
-      user.reload
     end
 
-    it "should update the users settings" do
-      user.name.should == 'mo khan'
-      user.email.should == 'mo@mokhan.ca'
-      user.city.should == 'Calgary'
-      user.website.should == 'http://mokhan.ca/'
-      user.twitter.should == 'mocheen'
-      user.facebook.should == 'fb'
+    it "updates the users settings" do
+      user.reload
+      expect(user.name).to eql('mo khan')
+      expect(user.email).to eql('mo@mokhan.ca')
+      expect(user.city).to eql('Calgary')
+      expect(user.website).to eql('http://mokhan.ca/')
+      expect(user.twitter).to eql('mocheen')
+      expect(user.facebook).to eql('fb')
     end
 
-    it "should redirect to the settings page" do
-      response.should redirect_to(my_settings_path)
+    it "redirects to the settings page" do
+      expect(response).to redirect_to(my_settings_path)
     end
 
-    it "should include a success message" do
-      flash[:notice].should == I18n.t(:profile_saved)
+    it "includes a success message" do
+      expect(flash[:notice]).to eql(I18n.t(:profile_saved))
     end
   end
 end
spec/controllers/comments_controller_spec.rb
@@ -12,9 +12,9 @@ describe CommentsController do
     it "creates a new comment" do
       post :create, id: 1207743539, url: "http://localhost:3000/creations/#{creation.to_param}", comment: { id: 1207743539, text: 'very nice' }
       comment = Comment.last
-      comment.disqus_id.should == 1207743539
-      comment.creation.should == creation
-      comment.text.should == 'very nice'
+      expect(comment.disqus_id).to eql(1207743539)
+      expect(comment.creation).to eql(creation)
+      expect(comment.text).to eql('very nice')
     end
   end
 end
spec/controllers/home_controller_spec.rb
@@ -3,11 +3,11 @@ require "rails_helper"
 describe HomeController do
   it "should render the about us page" do
     get "about_us"
-    response.should be_success
+    expect(response).to be_success
   end
 
   it "should render the why cakeside page" do
     get "why_cakeside"
-    response.should be_success
+    expect(response).to be_success
   end
 end
spec/controllers/registrations_controller_spec.rb
@@ -5,7 +5,7 @@ describe RegistrationsController do
     let(:user_session) { double(access: SecureRandom.hex(12)) }
 
     it 'creates a new user' do
-      User.stub(:login).with('mo@cakeside.com', 'password').and_return(user_session)
+      allow(User).to receive(:login).with('mo@cakeside.com', 'password').and_return(user_session)
 
       post :create, user: { name: 'mo', email: 'mo@cakeside.com', password: 'password' }
 
spec/controllers/sessions_controller_spec.rb
@@ -29,7 +29,7 @@ describe SessionsController do
       let(:password) { "password" }
 
       before :each do
-        User.stub(:login).with(username, password).and_return(user_session)
+        allow(User).to receive(:login).with(username, password).and_return(user_session)
         post :create, session: { username: username, password: password }
       end
 
@@ -45,7 +45,7 @@ describe SessionsController do
 
     context "when the username is not known" do
       before :each do
-        User.stub(:login).and_return(nil)
+        allow(User).to receive(:login).and_return(nil)
       end
 
       it "returns an error" do
@@ -61,7 +61,7 @@ describe SessionsController do
 
     before :each do
       request.cookies[:raphael] = user_session.key
-      controller.stub(:user_session).and_return(user_session)
+      allow(controller).to receive(:user_session).and_return(user_session)
       delete :destroy, id: "me"
     end
 
spec/controllers/tutorials_controller_spec.rb
@@ -14,7 +14,7 @@ describe TutorialsController do
     end
 
     it "assigns all tutorials as @tutorials" do
-      assigns(:tutorials).should include(tutorial)
+      expect(assigns(:tutorials)).to match_array([tutorial])
     end
   end
 
@@ -27,7 +27,7 @@ describe TutorialsController do
     end
 
     it "assigns the requested tutorial" do
-      assigns(:tutorial).should == tutorial
+      expect(assigns(:tutorial)).to eql(tutorial)
     end
   end
 end
spec/features/change_password_spec.rb
@@ -21,7 +21,7 @@ describe "changing my password", js: true do
     end
 
     it "should display a confirmation message" do
-      page.should have_content(I18n.translate('passwords.updated'))
+      expect(page).to have_content(I18n.translate('passwords.updated'))
     end
   end
 end
spec/features/change_profile_settings_spec.rb
@@ -21,6 +21,6 @@ describe "Change settings", js: true do
   end
 
   it "should save the changes properly" do
-    page.should have_content(I18n.translate(:profile_saved))
+    expect(page).to have_content(I18n.translate(:profile_saved))
   end
 end
spec/features/forgot_password_spec.rb
@@ -14,7 +14,7 @@ describe "password retrieval", :js => true do
     end
 
     it "should send them an email with instructions" do
-      page.should have_content(I18n.t('devise.passwords.send_instructions'))
+      expect(page).to have_content(I18n.t('devise.passwords.send_instructions'))
     end
   end
 
spec/features/logins_spec.rb
@@ -4,7 +4,7 @@ describe "Logins" do
   describe "GET /login" do
     it "should be able to reach the login page" do
       visit login_path
-      page.should have_content("Got an account? Login!")
+      expect(page).to have_content("Got an account? Login!")
     end
 
     context "when an email is registered", :js => true do
@@ -20,11 +20,11 @@ describe "Logins" do
       end
 
       it "should let the user signin with the proper password" do
-        page.should have_content("Log Out")
+        expect(page).to have_content("Log Out")
       end
 
       it "should not have an error" do
-        page.should_not have_content(I18n.t('devise.failure.invalid'))
+        expect(page).to_not have_content(I18n.t('devise.failure.invalid'))
       end
     end
 
@@ -39,7 +39,7 @@ describe "Logins" do
       end
 
       it "should display an error message" do
-        page.should have_content("invalid email or password.")
+        expect(page).to have_content("invalid email or password.")
       end
     end
   end
spec/features/tutorials_spec.rb
@@ -4,7 +4,7 @@ describe "Tutorials" do
   describe "GET /tutorials" do
     it "works! (now write some real specs)" do
       visit tutorials_path
-      page.should have_content("CakeSide")
+      expect(page).to have_content("CakeSide")
     end
   end
 end
spec/mailers/notification_mailer_spec.rb
@@ -6,19 +6,19 @@ describe NotificationMailer do
     let(:mail) { NotificationMailer.notification_email(user) }
 
     it "adds a subject" do
-      mail.subject.should == "New Activity on CakeSide"
+      expect(mail.subject).to eql("New Activity on CakeSide")
     end
 
     it "sends to the users email" do
-      mail.to.should include user.email
+      expect(mail.to).to match_array([user.email])
     end
 
     it "should send from the correct address" do
-      mail.from.should include 'noreply@cakeside.com'
+      expect(mail.from).to match_array(['noreply@cakeside.com'])
     end
 
     it "includes their name" do
-      mail.body.encoded.should match(user.name)
+      expect(mail.body.encoded).to match(user.name)
     end
   end
 end
spec/mailers/user_mailer_spec.rb
@@ -6,19 +6,19 @@ describe UserMailer do
     let(:mail) { UserMailer.welcome_email(user) }
 
     it "adds a subject" do
-      mail.subject.should == "Welcome to CakeSide"
+      expect(mail.subject).to eql("Welcome to CakeSide")
     end
 
     it "sends to the users email" do
-      mail.to.should include user.email
+      expect(mail.to).to match_array([user.email])
     end
 
     it "should send from the correct address" do
-      mail.from.should include 'noreply@cakeside.com'
+      expect(mail.from).to match_array(['noreply@cakeside.com'])
     end
 
     it "includes their name" do
-      mail.body.encoded.should match(user.name)
+      expect(mail.body.encoded).to match(user.name)
     end
   end
 end
spec/models/comment_spec.rb
@@ -7,7 +7,7 @@ describe Comment do
 
     it "creates a new activity" do
       comment = Comment.create(user: user, creation: creation)
-      comment.activities.count.should == 1
+      expect(comment.activities.count).to eql(1)
     end
   end
 end
spec/models/creation_spec.rb
@@ -8,7 +8,7 @@ describe Creation do
       subject.name="HELLO WORLD"
       subject.category = create(:category)
       subject.save!
-      Creation.find(subject.id).name.should == "HELLO WORLD"
+      expect(Creation.find(subject.id).name).to eql("HELLO WORLD")
     end
   end
 
@@ -18,11 +18,11 @@ describe Creation do
     let(:results) { Creation.all }
 
     it "should load the newest first" do
-      results.first.should == newest
+      expect(results.first).to eql(newest)
     end
 
     it "should load the oldest last" do
-      results.last.should == oldest
+      expect(results.last).to eql(oldest)
     end
   end
 
@@ -35,7 +35,7 @@ describe Creation do
       let(:result) { creation.liked_by(user) }
 
       it "returns the existing favorite" do
-        result.should == favorite
+        expect(result).to eql(favorite)
       end
     end
 
@@ -43,10 +43,10 @@ describe Creation do
       let(:result) { creation.liked_by(user) }
 
       it "creates a new favorite" do
-        result.should_not be_nil
-        result.user.should == user
-        result.creation.should == creation
-        creation.reload.favorites.count.should == 1
+        expect(result).to_not be_nil
+        expect(result.user).to eql(user)
+        expect(result.creation).to eql(creation)
+        expect(creation.reload.favorites.count).to eql(1)
       end
     end
   end
spec/models/favorite_spec.rb
@@ -8,7 +8,7 @@ describe Favorite do
 
     it "creates a new activity" do
       user.favorites.create(creation: creation)
-      creation.author.activities.count.should == 1
+      expect(creation.author.activities.count).to eql(1)
     end
   end
 end
spec/models/photo_spec.rb
@@ -13,20 +13,20 @@ describe Photo do
     it "uploads each version to the blob storage" do
       subject.id = rand(100)
       subject.upload(file, blob_storage)
-      blob_storage.should have_received(:upload).with(upload_key, file)
-      blob_storage.should have_received(:upload).with(upload_key("large_"), file)
-      blob_storage.should have_received(:upload).with(upload_key("thumb_"), file)
+      expect(blob_storage).to have_received(:upload).with(upload_key, file)
+      expect(blob_storage).to have_received(:upload).with(upload_key("large_"), file)
+      expect(blob_storage).to have_received(:upload).with(upload_key("thumb_"), file)
     end
 
     it "sets the original filename" do
       subject.upload(file, blob_storage)
-      subject.original_filename.should == File.basename(file)
-      subject.image.should == File.basename(file)
+      expect(subject.original_filename).to eql(File.basename(file))
+      expect(subject.image).to eql(File.basename(file))
     end
 
     it "specifies the content type" do
       subject.upload(file, blob_storage)
-      subject.content_type.should == "image/jpeg"
+      expect(subject.content_type).to eql("image/jpeg")
     end
 
     it "applies the gps coordinates" do
spec/models/user_session_spec.rb
@@ -16,7 +16,7 @@ describe UserSession do
     let(:because) {  subject.access(request) }
 
     before :each do
-      Location.stub(:build_from_ip).with('192.168.1.1').and_return(location)
+      allow(Location).to receive(:build_from_ip).with('192.168.1.1').and_return(location)
       because
     end
 
spec/models/user_spec.rb
@@ -107,7 +107,7 @@ describe User do
     let(:mailer) { double("mailer", deliver_later: true) }
 
     before :each do
-      UserMailer.stub(:welcome_email).with(user).and_return(mailer)
+      allow(UserMailer).to receive(:welcome_email).with(user).and_return(mailer)
       user.send_welcome_email
     end
 
spec/routing/my/favorites_routing_spec.rb
@@ -1,9 +1,7 @@
 require 'rails_helper'
 
 describe My::FavoritesController do
-  describe "routing" do
-    it "routes to my favorites" do
-      get("/my/favorites").should route_to("my/favorites#index")
-    end
+  it "routes to my favorites" do
+    expect(get("/my/favorites")).to route_to("my/favorites#index")
   end
 end
spec/services/application/handlers/process_photo_spec.rb
@@ -7,7 +7,7 @@ describe ProcessPhoto do
 
   describe "#handles?" do
     it "handles photo uploads" do
-      subject.handles?(:upload_photo).should be_truthy
+      expect(subject.handles?(:upload_photo)).to be_truthy
     end
   end
 
@@ -16,7 +16,7 @@ describe ProcessPhoto do
     let(:photo) { double(upload: true, save!: true) }
 
     before :each do
-      photos.stub(:find).with(1).and_return(photo)
+      allow(photos).to receive(:find).with(1).and_return(photo)
       message = {
         photo_id: 1,
         file_path: image_path,
spec/services/handlers/publish_cake_to_twitter_spec.rb
@@ -8,7 +8,7 @@ describe PublishCakeToTwitter do
 
   describe "#handles?" do
     it "handles cake_published" do
-      subject.handles?(:cake_published).should be_truthy
+      expect(subject.handles?(:cake_published)).to be_truthy
     end
   end
 
@@ -18,29 +18,29 @@ describe PublishCakeToTwitter do
     let(:id) { 88 }
 
     before :each do
-      cake.stub(:user).and_return(artist)
-      cakes.stub(:find).with(id).and_return(cake)
+      allow(cake).to receive(:user).and_return(artist)
+      allow(cakes).to receive(:find).with(id).and_return(cake)
     end
 
     context "when the cake is published and safe for kids" do
       before :each do
-        cake.stub(:published?).and_return(true)
+        allow(cake).to receive(:published?).and_return(true)
       end
 
       it "tweets new cakes" do
         subject.handle(cake_id: id)
-        twitter.should have_received(:tweet).with("yummy By joe on http://www.blah.com/cakes/88-yummy!")
+        expect(twitter).to have_received(:tweet).with("yummy By joe on http://www.blah.com/cakes/88-yummy!")
       end
     end
 
     context "when the cake is not published" do
       before :each do
-        cake.stub(:published?).and_return(false)
+        allow(cake).to receive(:published?).and_return(false)
       end
 
       it "should not publish any tweets" do
         subject.handle(cake_id: id)
-        twitter.should_not have_received(:tweet)
+        expect(twitter).not_to have_received(:tweet)
       end
     end
   end
spec/services/infrastructure/exif_parser_spec.rb
@@ -7,19 +7,19 @@ describe ExifParser do
 
   it "parses the latitude and longitude" do
     latitude, longitude = subject.parse_geolocation_from(jpg_with_gps)
-    latitude.should == 51.07296369444445
-    longitude.should == -114.101799
+    expect(latitude).to eql(51.07296369444445)
+    expect(longitude).to eql(-114.101799)
   end
 
   it "ignores png files" do
     latitude, longitude = subject.parse_geolocation_from(png_file)
-    latitude.should be_nil
-    longitude.should be_nil
+    expect(latitude).to be_nil
+    expect(longitude).to be_nil
   end
 
   it "ignores jpg files with no gps info" do
     latitude, longitude = subject.parse_geolocation_from(jpg_no_gps)
-    latitude.should be_nil
-    longitude.should be_nil
+    expect(latitude).to be_nil
+    expect(longitude).to be_nil
   end
 end
spec/services/infrastructure/image_spec.rb
@@ -25,7 +25,7 @@ describe Image do
 
     it "parses the geolocation info" do
       coordinates = [rand(100), rand(50)]
-      exif.stub(:parse_geolocation_from).with('blah.jpg').and_return(coordinates)
+      allow(exif).to receive(:parse_geolocation_from).with('blah.jpg').and_return(coordinates)
       expect(subject.geolocation).to eql(coordinates)
     end
   end
spec/support/authentication.rb
@@ -1,8 +1,8 @@
 module Authentication
   def http_login(user)
     new_session = UserSession.new
-    controller.stub(:user_session).and_return(new_session)
-    controller.stub(:current_user).and_return(user)
-    controller.stub(:user_signed_in?).and_return(true)
+    allow(controller).to receive(:user_session).and_return(new_session)
+    allow(controller).to receive(:current_user).and_return(user)
+    allow(controller).to receive(:user_signed_in?).and_return(true)
   end
 end
spec/spec_helper.rb
@@ -63,8 +63,8 @@ RSpec.configure do |config|
     # Enable only the newer, non-monkey-patching expect syntax.
     # For more details, see:
     #   - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
-    #expectations.syntax = :expect
-    expectations.syntax = [:should, :expect]
+    expectations.syntax = :expect
+    #expectations.syntax = [:should, :expect]
   end
 
   # rspec-mocks config goes here. You can use an alternate test double
@@ -73,8 +73,8 @@ RSpec.configure do |config|
     # Enable only the newer, non-monkey-patching expect syntax.
     # For more details, see:
     #   - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
-    #mocks.syntax = :expect
-    mocks.syntax = [:should, :expect]
+    mocks.syntax = :expect
+    #mocks.syntax = [:should, :expect]
 
     # Prevents you from mocking or stubbing a method that does not exist on
     # a real object. This is generally recommended.