RSpec 3 syntax
[arvados.git] / spec / google / api_client / result_spec.rb
index dea1bc1542b8dc6492f6fe5d1b2cf8fd84d1c08e..67c63b77cfcc34472ad02226949c70f8d068c00e 100644 (file)
 require 'spec_helper'
 
 require 'google/api_client'
-require 'google/api_client/version'
 
-describe Google::APIClient::Result do
-  CLIENT ||= Google::APIClient.new
+RSpec.describe Google::APIClient::Result do
+  CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
 
   describe 'with the plus API' do
     before do
@@ -34,10 +33,10 @@ describe Google::APIClient::Result do
       })
       @request = @reference.to_http_request
 
-      # Response stub
-      @response = stub("response")
-      @response.stub(:status).and_return(200)
-      @response.stub(:headers).and_return({
+      # Response double
+      @response = double("response")
+      allow(@response).to receive(:status).and_return(200)
+      allow(@response).to receive(:headers).and_return({
         'etag' => '12345',
         'x-google-apiary-auth-scopes' =>
           'https://www.googleapis.com/auth/plus.me',
@@ -51,7 +50,7 @@ describe Google::APIClient::Result do
 
     describe 'with a next page token' do
       before do
-        @response.stub(:body).and_return(
+        allow(@response).to receive(:body).and_return(
           <<-END_OF_STRING
           {
             "kind": "plus#activityFeed",
@@ -61,7 +60,7 @@ describe Google::APIClient::Result do
             "nextLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN",
             "title": "Plus Public Activity Feed for ",
             "updated": "2012-04-23T00:00:00.000Z",
-            "id": "tag:google.com,2010:/plus/people/foo/activities/public",
+            "id": "123456790",
             "items": []
           }
           END_OF_STRING
@@ -70,48 +69,49 @@ describe Google::APIClient::Result do
       end
 
       it 'should indicate a successful response' do
-        @result.error?.should be_false
+        expect(@result.error?).to be_falsey
       end
 
       it 'should return the correct next page token' do
-        @result.next_page_token.should == 'NEXT+PAGE+TOKEN'
+        expect(@result.next_page_token).to eq('NEXT+PAGE+TOKEN')
       end
 
       it 'should escape the next page token when calling next_page' do
-        pending("This is caused by Faraday's encoding of query parameters.")
         reference = @result.next_page
-        Hash[reference.parameters].should include('pageToken')
-        Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
-        url = reference.to_env(Faraday.default_connection)[:url]
-        url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
+        expect(Hash[reference.parameters]).to include('pageToken')
+        expect(Hash[reference.parameters]['pageToken']).to eq('NEXT+PAGE+TOKEN')
+        url = reference.to_env(CLIENT.connection)[:url]
+        expect(url.to_s).to include('pageToken=NEXT%2BPAGE%2BTOKEN')
       end
 
       it 'should return content type correctly' do
-        @result.media_type.should == 'application/json'
+        expect(@result.media_type).to eq('application/json')
       end
 
       it 'should return the result data correctly' do
-        @result.data?.should be_true
-        @result.data.class.to_s.should ==
+        expect(@result.data?).to be_truthy
+        expect(@result.data.class.to_s).to eq(
             'Google::APIClient::Schema::Plus::V1::ActivityFeed'
-        @result.data.kind.should == 'plus#activityFeed'
-        @result.data.etag.should == 'FOO'
-        @result.data.nextPageToken.should == 'NEXT+PAGE+TOKEN'
-        @result.data.selfLink.should ==
+        )
+        expect(@result.data.kind).to eq('plus#activityFeed')
+        expect(@result.data.etag).to eq('FOO')
+        expect(@result.data.nextPageToken).to eq('NEXT+PAGE+TOKEN')
+        expect(@result.data.selfLink).to eq(
             'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
-        @result.data.nextLink.should ==
+        )
+        expect(@result.data.nextLink).to eq(
             'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
             'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
-        @result.data.title.should == 'Plus Public Activity Feed for '
-        @result.data.id.should ==
-            'tag:google.com,2010:/plus/people/foo/activities/public'
-        @result.data.items.should be_empty
+        )
+        expect(@result.data.title).to eq('Plus Public Activity Feed for ')
+        expect(@result.data.id).to eq("123456790")
+        expect(@result.data.items).to be_empty
       end
     end
 
     describe 'without a next page token' do
       before do
-        @response.stub(:body).and_return(
+        allow(@response).to receive(:body).and_return(
           <<-END_OF_STRING
           {
             "kind": "plus#activityFeed",
@@ -119,7 +119,7 @@ describe Google::APIClient::Result do
             "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
             "title": "Plus Public Activity Feed for ",
             "updated": "2012-04-23T00:00:00.000Z",
-            "id": "tag:google.com,2010:/plus/people/foo/activities/public",
+            "id": "123456790",
             "items": []
           }
           END_OF_STRING
@@ -128,31 +128,32 @@ describe Google::APIClient::Result do
       end
 
       it 'should not return a next page token' do
-        @result.next_page_token.should == nil
+        expect(@result.next_page_token).to eq(nil)
       end
 
       it 'should return content type correctly' do
-        @result.media_type.should == 'application/json'
+        expect(@result.media_type).to eq('application/json')
       end
 
       it 'should return the result data correctly' do
-        @result.data?.should be_true
-        @result.data.class.to_s.should ==
+        expect(@result.data?).to be_truthy
+        expect(@result.data.class.to_s).to eq(
             'Google::APIClient::Schema::Plus::V1::ActivityFeed'
-        @result.data.kind.should == 'plus#activityFeed'
-        @result.data.etag.should == 'FOO'
-        @result.data.selfLink.should ==
+        )
+        expect(@result.data.kind).to eq('plus#activityFeed')
+        expect(@result.data.etag).to eq('FOO')
+        expect(@result.data.selfLink).to eq(
             'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
-        @result.data.title.should == 'Plus Public Activity Feed for '
-        @result.data.id.should ==
-            'tag:google.com,2010:/plus/people/foo/activities/public'
-        @result.data.items.should be_empty
+        )
+        expect(@result.data.title).to eq('Plus Public Activity Feed for ')
+        expect(@result.data.id).to eq("123456790")
+        expect(@result.data.items).to be_empty
       end
     end
-    
+
     describe 'with JSON error response' do
       before do
-        @response.stub(:body).and_return(
+        allow(@response).to receive(:body).and_return(
          <<-END_OF_STRING
          {
           "error": {
@@ -169,37 +170,37 @@ describe Google::APIClient::Result do
          }
          END_OF_STRING
         )
-        @response.stub(:status).and_return(400)
+        allow(@response).to receive(:status).and_return(400)
         @result = Google::APIClient::Result.new(@reference, @response)
       end
-      
+
       it 'should return error status correctly' do
-        @result.error?.should be_true
+        expect(@result.error?).to be_truthy
       end
 
       it 'should return the correct error message' do
-        @result.error_message.should == 'Parse Error'
+        expect(@result.error_message).to eq('Parse Error')
       end
     end
-    
+
     describe 'with 204 No Content response' do
       before do
-        @response.stub(:body).and_return('')
-        @response.stub(:status).and_return(204)
-        @response.stub(:headers).and_return({})
+        allow(@response).to receive(:body).and_return('')
+        allow(@response).to receive(:status).and_return(204)
+        allow(@response).to receive(:headers).and_return({})
         @result = Google::APIClient::Result.new(@reference, @response)
       end
 
       it 'should indicate no data is available' do
-        @result.data?.should be_false
+        expect(@result.data?).to be_falsey
       end
-      
+
       it 'should return nil for data' do
-        @result.data.should == nil
+        expect(@result.data).to eq(nil)
       end
-      
+
       it 'should return nil for media_type' do
-        @result.media_type.should == nil
+        expect(@result.media_type).to eq(nil)
       end
     end
   end